Jump to content

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);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.