The accepted answer is correct, I am looking to provide clarification as to why the OP was not receiving the expected results with NumberFormatter
.
NumberFormatter::parse
will output
a float
and accepts a string
argument in the specified locale format. Parse however does not format the output.
It is the equivalent of performing echo (float) '5.20'; //(float) 5.2
NumberFormatter::format
will output
a string
in the specified locale format and accepts an integer
or float
argument.
It is the equivalent of performing echo number_format(5.2, 2); //(string) "5.20"
However number_format
uses default values as the fraction and thousands separators and they would need to be changed based on desired display rules.
NumberFormatter
requires that you call the parse
method prior to calling the format
method, for it to convert the locale specific string value to the integer or float value equivalent and have a separate NumberFormatter
for your desired output.
$lv = new \NumberFormatter('lv', \NumberFormatter::DECIMAL);
$lv->setAttribute($input::FRACTION_DIGITS, 2); //applies rounding during format
$en = new \NumberFormatter('en', \NumberFormatter::DECIMAL);
$en->setAttribute($output::FRACTION_DIGITS, 2); //applies rounding during format
foreach (['5,77', '5,20', '5,2', '5,00', '5', '0'] as $value) {
$parsed = $lv->parse($value);
var_dump($parsed); //convert locale string to float value
var_dump($en->format($parsed)); //convert float to locale string
var_dump($lv->format($parsed)); //convert float to locale string
echo "<br/>\n";
}
$lv::parse($value)
+-------+--------+----------+-------+
| Value | Parse | EXPECTED | VALID |
+-------+--------+----------+-------+
| 5,77 | 5.77 | 5.77 | YES |
| 5,20 | 5.2 | 5.2 | YES |
| 5,2 | 5.2 | 5.2 | YES |
| 5,00 | 5 | 5 | YES |
| 5 | 5 | 5 | YES |
| 0 | 0 | 0 | YES |
+-------+--------+----------+-------+
$en::format($parsed)
+-------+--------+----------+-------+
| Value | Format | EXPECTED | VALID |
+-------+--------+----------+-------+
| 5,77 | 5.77 | 5.77 | YES |
| 5,20 | 5.20 | 5.20 | YES |
| 5,2 | 5.20 | 5.20 | YES |
| 5,00 | 5.00 | 5.00 | YES |
| 5 | 5.00 | 5.00 | YES |
| 0 | 0.00 | 0.00 | YES |
+-------+--------+----------+-------+
$lv::format($parsed)
+-------+--------+----------+-------+
| Value | Format | EXPECTED | VALID |
+-------+--------+----------+-------+
| 5,77 | 5,77 | 5,77 | YES |
| 5,20 | 5,20 | 5,20 | YES |
| 5,2 | 5,20 | 5,20 | YES |
| 5,00 | 5,00 | 5,00 | YES |
| 5 | 5,00 | 5,00 | YES |
| 0 | 0,00 | 0,00 | YES |
+-------+--------+----------+-------+