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
https://forums.phpfreaks.com/topic/172669-solved-number_format-problem/
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.

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.