-1

I would like to use the number_format() function without to specify the number of decimals. (If 2 decimals, display 2, if 5, display 5)

Is that possible?

Thanks a lot

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Flatbeat
  • 123
  • 1
  • 3
  • 13
  • 6
    Then just echo the float? https://3v4l.org/eZ5dt – Andreas Mar 15 '18 at 14:12
  • 5
    To you, I grant permission to use `number_format` function without specifying the number of decimals. Please, try it my son. For you, it shall work. – N.B. Mar 15 '18 at 14:12
  • have you tried before asking? it requires less effort! – Lelio Faieta Mar 15 '18 at 14:16
  • I'm sorry but with this, it's not working. I want to keep the format :number_format($number, null, ',', ' ') – Flatbeat Mar 15 '18 at 14:17
  • 1
    @N.B. If you pass a number to `number_format()` without defining how many decimals (only use one argument), the function will return an integer (removing any decimals). If you actually want to format the number (setting a different decimal separator/thousand separator), then you _have to_ define how many decimals you want. You simply can't do what the OP asks about with just `number_format()`. – M. Eriksson Mar 15 '18 at 14:20
  • @MagnusEriksson I'm sorry that you didn't get the joke, but thanks for the explanation, I'm lost at words so I can't thank you properly :). Did you get the part where OP doesn't even need any kind of function to do what he wants or did you get lost in explaining? You silly SJW's. – N.B. Mar 15 '18 at 14:27
  • @N.B. That joke went over my head. As I read it, the OP wants to format a decimal number. Example: the decimal `1234.12` should be printed as `1 234,12` or `1.234,12` or what ever. Then `number_format()` makes sense and just echoing the variable wouldn't work either. In what way would the OP then _"not need any kind of function"_? – M. Eriksson Mar 15 '18 at 14:32
  • @MagnusEriksson check the first comment, that's what I'm referring to. I get that the OP has the issue where he wants to feed a function with random number and get unified result back, but since he was inconclusive with his question I made a silly joke. Nevertheless, you're correct in your observation and I've nothing to add really :) – N.B. Mar 15 '18 at 14:36
  • @N.B. Fair enough. We couldn't survive here on SO if we didn't make silly jokes now and again. :-) – M. Eriksson Mar 15 '18 at 14:38

2 Answers2

1

If this is really what you want, this may work even if this is totally useless

function numberOfDecimals($value)
{
    if ((int)$value == $value)
    {
        return 0;
    }
    else if (! is_numeric($value))
    {
        // throw new Exception('numberOfDecimals: ' . $value . ' is not a number!');
        return false;
    }

    return strlen($value) - strrpos($value, '.') - 1;
}

$number = 1.23456;
print number_format($number,numberOfDecimals($number));

credits : PHP: get number of decimal digits

Gourgandine
  • 189
  • 6
1

You can calculate the number of decimals before you use numberformat and use that in the function.

$number = 50001/4;

If(strpos($number, ".") !== false){
    // Is float
    $decimals = strlen($number) - (strpos($number, ".")+1);
}Else{
    // Is integer == no decimals
    $decimals =0;
}

Echo number_format($number, $decimals, ',', ' ');

https://3v4l.org/DULp4

Andreas
  • 23,610
  • 6
  • 30
  • 62