Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

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

solved it with this

 

$newamount     = explode(",","$amount");
$str =  "$newamount[0]$newamount[1]";
$newamount     = stripslashes($str);
$newamount     = mysql_real_escape_string($newamount);

 

but surely there is a function that does this??

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

 

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.