0

I'm retrieving values from MySQL numeric field and want to format with a comma separator for 1000s like this: 21,000

This code below seems to work for display - how do I strip out the commas again before updating and inserting into MySQL DB?

<input type="text" name="Price id="Price value="<?php echo number_format($row_['Price'])); ?>" size="10" />

Thanks .........

2 Answers2

1

You can use the numberFormatter class for both making and stripping the formatting around values quite nicely:

You can format easily with format()

<?php
$fmt = numfmt_create( 'de_DE', NumberFormatter::DECIMAL );
$data = numfmt_format($fmt, 1234567.891234567890000);
if(intl_is_failure(numfmt_format($fmt))) {
    report_error("Formatter error");
}
?>

Output

1.234.567,891

Then, the parse() function will let you remove whatever formatting you applied to get back to your original format.

<?php
$fmt = numfmt_create( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo numfmt_parse($fmt, $num)."\n";
echo numfmt_parse($fmt, $num, NumberFormatter::TYPE_INT32)."\n";
?>

Output:

1234567.891

Note: Keep in mind that if you are passing these things in forms and back and forth, you will potentially lose decimal places or the like if you format them down.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • I don't want decimals just comma separators for thousands which number_format() does ... I'm just trying work out the correct syntax before updating/inserting the value into MySQL DB and stripping out the commas again ... – user3529414 Jun 01 '14 at 05:00
  • Numberformatter doesn't just do decimals. It will format to whatever you like, then back to what you had very nicely. – Fluffeh Jun 01 '14 at 05:00
  • You're not getting me - I know what number_format does and how to use it ... I'm trying tow work out how to strip out the commas for the formated value in inpout field before inseting/updating MySQL DB – user3529414 Jun 01 '14 at 05:20
  • 1
    @user3529414 No, you aren't getting me. [NumberFormatter](http://www.php.net/manual/en/class.numberformatter.php) is NOT `number_format()`. – Fluffeh Jun 01 '14 at 05:28
0

Not sure I understood your question correctly, but you could remove the character with str_replace...

str_replace(",", "", $Price);
Juri Salminen
  • 131
  • 1
  • 5