Jump to content

[SOLVED] number_format problem


StefanRSA

Recommended Posts

I am not to sure how to go around this problem....

I have a field where a user must put in a number.

This is a actually a field used for currency but no calculation is done. So i am using textfield.

Now I am trying to figure out how I can make it display in a standard format using:

$value= trim($_POST['rand']);
$rand = number_format ($value, 0);

 

All works well if a user enters a value like 100000. But when a user enters 100,000 only 100 gets posted and am sure its because of the "number_format"....

 

What will be the best to work with this problem?

 

Thanks

Link to comment
Share on other sites

All works well if a user enters a value like 100000. But when a user enters 100,000 only 100 gets posted and am sure its because of the "number_format"....

It's not number format that's the problem. It's the fact that the posted value is a string. number_format expects a number, and standard PHP behaviour is to cast the string to a numeric value.... by taking any only leading digits.

So "13 monkeys" would give 13, because the space character isn't a digit, and "100,000" will give 100 because the comma (,) isn't a digit.

 

Follow btherl's suggestion which strips out all non-digits from the string, before using number_format. You may need to modify the regular expression to allow for a decimal point though.

Link to comment
Share on other sites

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.