Jump to content

[SOLVED] number_format problem


shadiadiph

Recommended Posts

here is the problem.

 

I have a form where the user can input an amount if they enter say 100,000.00  or 100,000 it will save to the database as 100.00 how do i make it so it will save to the database as 100000.00

 

here is what I have just tried it doesn't work

 

$amount     = ($_POST["amount"]);
$newamount     = number_format($amount);
$newamount     = stripslashes($newamount);
$newamount     = mysql_real_escape_string($newamount);

 

then i am trying to insert $newamount

Link to comment
https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/
Share on other sites

  Quote

100,000.00 will be evaluated as a string...

 

use

 

$amount = preg_replace('/[0-9\.]/',,$_POST['amount']);

 

should get round all that extra code...

 

 

 

Errrr???  What?

 

 

Won't that leave anything that's not a number or ,?  In other words, wouldn't that return "," for "500,000"?

 

 

Why not just do:

 

preg_replace('[^0-9]', '', $number);

 

If storing in the database - make sure your datatype is a float (n,2)

 

That way you can store the value from PHP inside MySQL and it should store as a number. This will allow you to perform calculations on the value directly (via MySQL) without worry.

 

Only when displaying in the browser do you need to worry about formatting.

$value=100000;
echo number_format($value,2);

 

That WILL output 100,000.00

That doesn't make sense.

 

If you're already storing your values inside a MySQL datatype of float(n,2) then just use number_format($val,2) to format the contents of $val to 2 decimal places.

 

Change the 2 for another value

ie. number_format($val,n); (n decimal places)

  Quote

  Quote

100,000.00 will be evaluated as a string...

 

use

 

$amount = preg_replace('/[0-9\.]/',,$_POST['amount']);

 

should get round all that extra code...

 

 

Errrr???  What?

 

 

Won't that leave anything that's not a number or ,?  In other words, wouldn't that return "," for "500,000"?

 

 

Why not just do:

 

preg_replace('[^0-9]', '', $number);

 

 

erms oooops - good spot!

 

actually I was just testing...

 

also make sure you keep the decimal point!!!

 

preg_replace('/[^0-9\.]/', '', $number);

 

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.