I have two types of numbers (FLOAT and BIGINT) in a mysql table.
A:0.002
B: 51443234
I need to replace “.” with “,” in the numbers and I can do it by number_format
foreach ($row as $key => $val) {
if (is_numeric($val)) {
$row[$key] = number_format($val,'3', ',', '.'); }
}
}
new format is:
A: 0,002
B:51.443.234,000
but I don’t want the trailing zeros in number B, so I try to format FLOAT and BIGINT values separately:
foreach ($row as $key => $val) {
if (is_numeric($val)) {
if (is_float($val)) {
$row[$key] = number_format($val,'3', ',', '.'); }
else {
$row[$key] = number_format($val,'0', ',', '.'); }
}
}
but this doesn’t work.
I’m not sure where the problem is and if I can do it without converting values to string.
(I don’t know what is the disadvantage of converting to strings either..)
Can you please show the right approach?
Thank you.
PHP is a loosely typed language, therefore it doesn’t know about FLOATS and BIGINTS. In fact, neither
0,002or51.443.234are valid values.For doing calculations, don’t change anything. For displaying your numbers, format them however you want, even if they’re strings.
http://codepad.org/epIuX8MZ