1

$numval = 12345.50;

Desired output:

12 345,50

The comma instead of a dot is not a problem but how can I get the thousands separator to be a white-space?

I noticed PHP money format with spaces but this is not a duplicate post. Using number_format is out of question as it rounds the input value. I can't allow the values passed through it to be rounded at all.

Is there a built-in way to do exactly what number_format() does, but without rounding the value or do I have to write my own function to do this?

Community
  • 1
  • 1
BudwiseЯ
  • 1,846
  • 2
  • 16
  • 28
  • 2
    Just a side-note - I tend to lean toward number_format() for all cases (including formatting money) because [money_format()](http://php.net/manual/en/function.money-format.php) depends on strfmon which not all operating systems have *cough* windows! *cough* so code that uses it may not work on all servers. – None May 30 '12 at 18:32

3 Answers3

3

If rounding is out of the question, so is float values. You must go back to integers if you don't want rounding since floating-point arithmetic is not exact. In that case you'll have to implement the formatting function yourself.

This is especially true if you are handling money. See for example Why not use Double or Float to represent currency?

Community
  • 1
  • 1
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • Not exactly what I asked, but included so important point that I'll have to accept this. Never using floats or doubles to handle money again. Never. Thank you. – BudwiseЯ Jun 27 '12 at 15:17
2

This looks like the version of the function you want to use:

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

So for example:

$newNumber = number_format($oldNumber, 2, ",", " ");

For more information check out http://php.net/manual/en/function.number-format.php

dpk2442
  • 701
  • 3
  • 8
  • Which parameter needs to be set in which way to not make `number_format` round the value? – hakre May 30 '12 at 20:36
  • 1
    `int $decimals = 0` That sets how many places it rounds to. The default it 0, which means no decimals, 1 means one place of precision, and so forth. – dpk2442 May 31 '12 at 01:40
  • I ask how to disable rounding, you told me how to do rounding. Does that mean, you don't know how to disable rounding with `number_format`? Did I misread the question that it's asking about **how to not round**? – hakre May 31 '12 at 06:43
  • 1
    If you are using `number_format()` it will round. However you can specify how many places it rounds to. So you could set the number to something really high, say 10, and then it would round after 10 decimal places. Meaning it wouldn't really be rounding all that much. – dpk2442 May 31 '12 at 11:53
  • Well there is a difference between rounding and not rounding, so even I can see your good intentions, please don't turn reality over only to make a point. TS explicitly asked for that so I'd say s/he did this for a reason. – hakre May 31 '12 at 12:26
  • Thank you for your input. However I believe my answer will work in this case, and if it does not, TS will find an alternate solution. Because you can set the number of decimals to a very large value, it is hard for me to imagine a situation where this will not work. Also, if it is only being used to display the information, applications do not usually show more than a few decimals to the user, even if they store more. – dpk2442 May 31 '12 at 15:53
2

From this comment of the number_format() page (I modified the function to match the number_format defaults though).

To prevent rounding:

function fnumber_format($number, $decimals=0, $dec_point='.', $thousands_sep=',') {
        if (($number * pow(10 , $decimals + 1) % 10 ) == 5)  //if next not significant digit is 5
            $number -= pow(10 , -($decimals+1));

        return number_format($number, $decimals, $dec_point, $thousands_sep);
}
tplaner
  • 8,363
  • 3
  • 31
  • 47