Jump to content

converting an input amount to valid float for db update


coderb

Recommended Posts

Hi All,

 

I'm trying to find the correct method for this small problem.

I have an edit form that displays a mysql value (column type: float) in an input field. this is a price field that is displayed using this code:

value="<?php echo number_format($rowSQL['price'],2)?>"

 

so the price is displayed as eg: $ 1,984.56

 

now the user updates the price value to $2,000.00 and submits. (sometimes they may include the comma, sometimes not.)

 

what is the correct way to save this value to the database? Do I need to use a php string function to remove the comma, or is there a setting that saves this as is to the db, correctly?

 

thanks for the info, thought there just might be a db setting or something that recognizes this as a number fomat, but I guess that since the comma is generated by a php string function it is purely for display, so a reverse function is necessary on db update.

 

thanks for the help

Yeah, it doesn't like any non float characters...

 

You could also use filter var to get rid of any non-float characters.

 

<?php

$number="$2,500.00";

 

// Filter anything that isn't a number, +/-/.

var_dump(filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT,

FILTER_FLAG_ALLOW_FRACTION));

?>

 

Should return

 

string(7) "2500.00"

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.