<<

NAME

ProductOpener::Numbers - normalize numbers sent as strings in various formats with different sets of separators for digit grouping and to indicate decimals.

DESCRIPTION

VARIABLES

$number_regexp

Regular expression to match something that looks like a number: 32 32.5 0.5 .5 32,5

FUNCTIONS

remove_insignificant_digits($)

Some apps send us nutrient values that they have stored internally as floating point numbers.

So we get values like:

2.9000000953674 1.6000000238419 0.89999997615814 0.359999990463256 2.5999999046326

On the other hand, when we get values like 2.0, 2.50 or 2.500, we want to keep the trailing 0s.

The goal is to keep the precision if it makes sense. The tricky part is that we do not know in advance how many significant digits we can have, it varies from products to products, and even nutrients to nutrients.

The desired output is thus:

2.9000000953674 -> 2.9 1.6000000238419 -> 1.6 0.89999997615814 -> 0.9 0.359999990463256 -> 0.36 2.5999999046326 -> 2.6 2 -> 2 2.0 -> 2.0 2.000 -> 2.000 2.0001 -> 2 0.0001 -> 0.0001

convert_string_to_number($)

Try to convert a number represented as a string to the actual number, by guessing which characters (spaces, commas, dots) are used as digit grouping separators or used to indicate decimals.

round_to_max_decimal_places($value, $max_decimal_places)

Round a number to a maximum number of decimal places.

Return undef if passed an undefined value.

<<