I don't know how to display my numbers to have commas and a decimal point in php..
I want 12345.90 to be displayed as 12,345.90
thanks in advance
You should check out number_format function. It will solve your problem.
Try this:
echo number_format(12345.90, 2, '.', ','); // Prints 12,345.90
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);
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'])
I have a very large number here but I cannot get rid of the notation. I want it to display only numbers instead of
"4.8357032784585E+24 - 77"
I tried (int)$variable but it didn't help. Suggestions?
Use number_format.
echo number_format($variable, 0, '.', '');
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