all,
I'm making a shopping cart that only accept input number with 1 decimal place.
The following is piece of code I get from a library (with some modification):
$items['qty'] = trim(preg_replace('/([^0-9\.])/i', '', $items['qty']));
This will accept any number. but I want to make it as only 1 decimal. how to modify the code?
Thanks
this can be done with round function
echo round(153.751, 1); // 153.8
this will help Rounding numbers with PHP
You can remove the decimal places after the first decimal place:
$items['qty'] = trim(preg_replace('/[^0-9\.]+|(?<=\.[0-9])[0-9+]+/', '', $items['qty']));
See it
You can use a regex like ^(\d+.{0,1}\d*)$
But PHP have a concept, the filter, that does a lot of validations for you, http://php.net/manual/en/filter.filters.validate.php
If i understand well the question, you can use number_format
$items['qty'] = number_format(trim(preg_replace('/([^0-9\.])/i', '', $items['qty'])),1,".","");
Hope it helps.
add a line:
$items['qty'] = sprintf('%.1f', $items['qty'])
Related
Servus,
I have a problem with float numbers in PHP. My number is
$cosVQ = 0.907424504992097
but when I do some math operations $cosVQ, I will receive different results in Javascript and PHP. Or for example wen I do echo I will receive only 0.9074245049920. What am I doing wrong?
You could use number_format:
echo number_format($cosVQ, 15);
Ok I found answer for my question, which works as I wanted: ini_set('precision', 15);
number_format stupidly rounds numbers by default. Is there just a simple way to turn off the rounding? I'm working with randomly generated numbers, and I could get things like...
42533 * .003 = 127.599 or,
42533 * .03 = 1275.99 or,
42533 * .3 = 12759.9
I need number_format (or something else) to express the result in traditional U.S. format (with a comma separating the thousands) and not round the decimal.
Any ideas?
The second argument of number_format is the number of decimals in the number. The easiest way to find that out is probably to treat it as a string (as per this question). Traditional US format is default behaviour, so you don't need to specify remaining arguments.
$num_decimals = strlen(substr(strrchr($number, "."), 1));
$formatted_number = number_format($number, $num_decimals);
If anyone's interested in this, I've written a little function to get around this problem.
With credit to Joel Hinz above, I came up with...
function number_format_with_decimals($value, $round_to){
//$value is the decimal number you want to show with number_format.
//$round_to is the deicimal place value you want to round to.
$round_to_decimals = strlen(substr(strrchr($value, "."), $round_to));
$ans = number_format($value, $round_to);
return $ans;
}
I tried these solutions, but what ended up being my answer was this:
$output=money_format('%!i', $value);
This gives you something like 3,234.90 instead of 3,234 or 3,234.9
I'm working with currencies and there are some items that cost say $35, some that are $35.50, etc. I'd like to take these numbers and format them to '3500' and if it's 35.50, it should be 3550.
I've tried
number_format($data['amount'], 2, '', '')
But if it's 35.50 it's 355000.
Any help would be great, thanks!
Did you try to multiply the number by 100?
Think of your math lessons:
echo $data['amount'] * 100;
look at the doc, then try this:
number_format($data['amount'], 2)
with those last two '' you were replacing the decimal separator with nothing and the thousands separator with nothing, which is something i don't believe you intended to do.
update
i see from the comments below that i misread your question.
I tried your code with demo here: http://codepad.org/uNAd84gG
and it seems to be in working order.
try:
number_format($amount, 2, ',', '.')
You can omit the last 2 to just have it behave by your locale
number_format($amount, 2);
I am having trouble thinking of a php function to add zeros after a decimal. Let say I have $money="10000" I need a function that will add .00 to 10000.00 and to add just a zero to 0 after 234.5.
can anyone help me please?
Just as Dan Grossman said; number_format is your friend here and you'd accomplish those double zeros by:
$money = "1234";
$formatedNumber = number_format($money, 2, '.', '');
echo $formatedNumber;
// 1234.00
The function is number_format
http://php.net/manual/en/function.number-format.php
Always search the PHP manual first :)
Maybe not the answer you want to hear, but I think you're better off storing these values in numeric types and formatting them properly just before output
I have a number stored in a variable. The number is pulled from the database as 9900..I need to convert this number to 99.00 in order to display to a customer in HTML.
I was wondering how can I achieve this in php.
Thanks.
$priceInCents = 9900;
$priceInDollars = $priceInCents / 100;
And then you can use round() or number_format() as you please.
You can use the number_format() function for that:
echo number_format(9900/100);
You can do this by either using money_format or number_format, if you are going to display it as a price you should look at money_format, otherwise, number_format is the way to go.