Numberformatter does not work MIN_FRACTION_DIGITS, 2

Asked

Viewed 66 times

1

Good use of 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;

He returns me 54,981 and I need you to return 54,98.

Someone knows how to fix this?

2 answers

2


Change MIN_FRACTION_DIGITS for MAX_FRACTION_DIGITS to define the maximum number of digits of 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;
  • 1

    thank you very much ;)

2

  • thank you very much ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.