How to change formatted numbers back to raw numbers in php
I'm using jquery number formatter to format numbers.
http://code.google.com/p/jquery-numberformatter/
$('#decnum').blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00", locale:"us开发者_如何学Go"});
});
I need the formatted numbers like this->232,000.00 for presentation. But what I need to store in the database shouldn't look like that. How do I transform it back in php?
If the commas are giving you trouble, try this:
$number = floatVal(str_replace(',', '', $numberString));
or if you want integers:
$number = intVal(str_replace(',', '', $numberString));
preg_replace will do what you want.
$db_number = preg_replace('/[^\d\.]/', '', $number);
echo $db_number; // 230000.00
$formattedNumber = "24,000.00";
$formattedNumber = explode('.',$formattedNumber); //24,000
$number = str_replace(",","",$formattedNumber[0]); // 24000
Another way of thinking : all the other answers are providing conversions, but let's suppose you have something more complicate : What about hiding the original data, and display only the nice number. When you're calling your saving code, you just retrieve the original data.
So your html will be something like this
<input type="hidden" name="original_data" value="232000" />
<input type="text" name="displayed_data" value="232000" id="#decnum" />
精彩评论