NumberFormatter does not work MIN_FRACTION_DIGITS, 2

1

I'm using php's NumberFormatter library to work with conversion, but I'm having a problem converting numbers with many digits after the.

Example:

$valores = '54.98054562';

$moeda1 = new NumberFormatter('pt_BR', NumberFormatter::DECIMAL);
$moeda1->setAttribute( NumberFormatter::MIN_FRACTION_DIGITS, 2);

$valores = $moeda1->formatCurrency($valores, 'BRL');

echo $valores;

It returns me 54,981 and needs to return 54.98.

Does anyone know how to solve this?

    
asked by anonymous 29.02.2016 / 15:14

2 answers

2

Change MIN_FRACTION_DIGITS by MAX_FRACTION_DIGITS to set the maximum number of digits in a fraction, in case 2.

<?php
$valores = '54.98054562';

$moeda1 = new NumberFormatter('pt_BR', NumberFormatter::DECIMAL);
$moeda1->setAttribute( NumberFormatter::MAX_FRACTION_DIGITS, 2);

$valores = $moeda1->formatCurrency($valores, 'BRL');

echo $valores;
    
29.02.2016 / 15:25
2

You are specifying a minimum of 2 digits for decimal places.

You should specify a maximum of 2 digits

$moeda1->setAttribute( NumberFormatter::MAX_FRACTION_DIGITS, 2);

see: link

    
29.02.2016 / 15:25