php - Selectional number formatting of array values


I have two types of numbers (FLOAT and BIGINT) in a mysql table.


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


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, neither0,002 or51.443.234 are valid values.

For doing calculations, don't change anything. For displaying your numbers, format them however you want, even if they're strings.

1. <?php
2. $float = 2.25;
3. $bigInt = 4235234;
5. $commaFloat = 2,25;
6. $commaBigInt = 4.235.234;
7. ?>

Parse error: syntax error, unexpected ',' on line 5

People are also looking for solutions to the problem: php - file_get_contents hangs, but I set timeout paramer


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.