Install via yarn add locutus and import:
import { round } from 'locutus/php/math/round'.
Or with CommonJS: const { round } = require('locutus/php/math/round')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
round(1241757, -3)
1242000
2
round(3.6)
4
3
round(2.835, 2)
2.84
4
round(1.1749999999999, 2)
1.17
5
round(58551.799999999996, 2)
58551.8
6
round(4096.485, 2)
4096.49
Dependencies
This function uses the following Locutus functions:
// the code is heavily based on the native PHP implementation // https://github.com/php/php-src/blob/PHP-7.4/ext/standard/math.c#L355
value = floatCast(value) precision = intCast(precision) p = Math.pow(10, precision)
if (isNaN(value) || !isFinite(value)) { return value }
// if value already integer and positive precision // then nothing to do, return early if (Math.trunc(value) === value && precision >= 0) { return value }
// PHP does a pre-rounding before rounding to desired precision // https://wiki.php.net/rfc/rounding#pre-rounding_to_the_value_s_precision_if_possible const preRoundPrecision = 14 - Math.floor(Math.log10(Math.abs(value)))
if (preRoundPrecision > precision && preRoundPrecision - 15 < precision) { value = roundToInt(value * Math.pow(10, preRoundPrecision), mode) value /= Math.pow(10, Math.abs(precision - preRoundPrecision)) } else { value *= p }
value = roundToInt(value, mode)
return value / p }
functionroundToInt(value, mode) { let tmp = Math.floor(Math.abs(value) + 0.5)
// the code is heavily based on the native PHP implementation // https://github.com/php/php-src/blob/PHP-7.4/ext/standard/math.c#L355
value = floatCast(value) precision = intCast(precision) p = Math.pow(10, precision)
if (isNaN(value) || !isFinite(value)) { return value }
// if value already integer and positive precision // then nothing to do, return early if (Math.trunc(value) === value && precision >= 0) { return value }
// PHP does a pre-rounding before rounding to desired precision // https://wiki.php.net/rfc/rounding#pre-rounding_to_the_value_s_precision_if_possible const preRoundPrecision = 14 - Math.floor(Math.log10(Math.abs(value)))
if (preRoundPrecision > precision && preRoundPrecision - 15 < precision) { value = roundToInt(value * Math.pow(10, preRoundPrecision), mode) value /= Math.pow(10, Math.abs(precision - preRoundPrecision)) } else { value *= p }
function_php_cast_int(value: CastIntValue): number { // original by: Rafał Kukawski
if (typeof value === 'number') { if (isNaN(value) || !isFinite(value)) { // from PHP 7, NaN and Infinity are casted to 0 return0 }
return value < 0 ? Math.ceil(value) : Math.floor(value) } if (typeof value === 'string') { returnparseInt(value, 10) || 0 }
// Behaviour for types other than float, string, boolean // is undefined and can change any time. // To not invent complex logic // that mimics PHP 7.0 behaviour // casting value->bool->number is used return +!!value }
// the code is heavily based on the native PHP implementation // https://github.com/php/php-src/blob/PHP-7.4/ext/standard/math.c#L355
value = floatCast(value) precision = intCast(precision) p = Math.pow(10, precision)
if (isNaN(value) || !isFinite(value)) { return value }
// if value already integer and positive precision // then nothing to do, return early if (Math.trunc(value) === value && precision >= 0) { return value }
// PHP does a pre-rounding before rounding to desired precision // https://wiki.php.net/rfc/rounding#pre-rounding_to_the_value_s_precision_if_possible const preRoundPrecision = 14 - Math.floor(Math.log10(Math.abs(value)))
if (preRoundPrecision > precision && preRoundPrecision - 15 < precision) { value = roundToInt(value * Math.pow(10, preRoundPrecision), mode) value /= Math.pow(10, Math.abs(precision - preRoundPrecision)) } else { value *= p }
if (typeof value === 'number') { if (isNaN(value) || !isFinite(value)) { // from PHP 7, NaN and Infinity are casted to 0 return0 }
return value < 0 ? Math.ceil(value) : Math.floor(value) } if (typeof value === 'string') { returnparseInt(value, 10) || 0 }
// Behaviour for types other than float, string, boolean // is undefined and can change any time. // To not invent complex logic // that mimics PHP 7.0 behaviour // casting value->bool->number is used return +!!value }
// the code is heavily based on the native PHP implementation // https://github.com/php/php-src/blob/PHP-7.4/ext/standard/math.c#L355
value = floatCast(value) precision = intCast(precision) p = Math.pow(10, precision)
if (isNaN(value) || !isFinite(value)) { return value }
// if value already integer and positive precision // then nothing to do, return early if (Math.trunc(value) === value && precision >= 0) { return value }
// PHP does a pre-rounding before rounding to desired precision // https://wiki.php.net/rfc/rounding#pre-rounding_to_the_value_s_precision_if_possible const preRoundPrecision = 14 - Math.floor(Math.log10(Math.abs(value)))
if (preRoundPrecision > precision && preRoundPrecision - 15 < precision) { value = roundToInt(value * Math.pow(10, preRoundPrecision), mode) value /= Math.pow(10, Math.abs(precision - preRoundPrecision)) } else { value *= p }
value = roundToInt(value, mode)
return value / p }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.