Jump to content

[SOLVED] Round to 4 decimal help


phpbeginner

Recommended Posts

I guess it's personal preference. However, sprintf is for strings and number_format is for numbers, so to me it makes more sense to "format a number" using the function that is designed to format numbers, rather than strings. Again, maybe it's just me. There's nothing "wrong" with sprintf, especially if you're used to using it more. I almost never use sprintf, because I mostly format numbers, so number_format is what I know.

It's not working, because round() function returns a float number. Whenever you echo a float, any zeroes at the end are omitted.

To echo float with exactly 4 places after a decimal separator you need to use one of the functions that will convert it to string.

This can be either

$n = sprintf('%.4f', $yourNumber );

 

or

 

$n = number_format($yourNumber,4);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.