This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how to print a number with 2 decimals, but only if there are decimals already?
Brothers I want to convert me any way I entered into a text field automatically turns
12 to 12.00
How possible work by php and javascript
Thanks
In PHP its easy to do that by doing:
number_format($numberVar, 2);
Try this number_format() from PHP.net:
echo number_format(12, 2)
What, no javascript?
var x = 3;
alert(x.toFixed(2)); // 3.00
Will "work" for the case given, but so will:
alert(x + '.00');
There are bugs in toFixed in some older browsers with certain values so many write their own simple routine.
$thisnum = $thisnum . ".00";
Proof of concept:
$thisnum = $thisnum . ".00";
echo ($thisnum + 4.55);
However, as pointed out by the community, this is something that will only work for the case mentioned in the question and will not handle any use cases that already have decimals. The other answers are better.
Related
This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 7 years ago.
So lets show an example here shall we?
Let's say I have 2 strings that are like this....
$math1 = "+300";
$math2 = "-125";
So obviously the answer would have to be +175 but how do I actually do the math while the input is a string.
I can't simply do
$math1 - $math2
Because it'd have to figure out if it is a + or a -, so how can this exactly be done?
You can use intval to get the integer value of a variable, and then you can do basic math operations.
<?php
$math1 = intval("+300");
$math2 = intval("-125");
echo $math1 - $math2;
?>
intval($math1) + intval($math2);
Can be:
$result = abs($math1) - abs($math2);
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 7 years ago.
I know it may be a silly one and it may be duplicate question as well, but I can't find any proper question posted before(may be its my incapability)
I have a value fetched from DB... its say like
4.5 which should be 4.500
0.01 which should be 0.010
11 which should be 11.000
How can I achieve this in PHP?
Simply format it with sprinft to make it a floating point number to 2 decimal places.
echo sprintf("%.2f", 4.5);
https://eval.in/378771
If you change the 2 to a 3 you will get your format you've listed.
echo sprintf("%.3f", 4.5) . PHP_EOL;
echo sprintf("%.3f", 0.01) . PHP_EOL;
echo sprintf("%.3f", 11) . PHP_EOL;
https://eval.in/378773
php.net/number_format
number_format(4.5,2);
Use the function number_format
Trailing zeros to the right of a decimal point is not supported in PHP. You need to format the number to string.
$value = number_format($value,3);
Modern way is to use NumberFormatter which formats numbers according to set locale.
$fmt = new NumberFormatter('en-US', NumberFormatter::DECIMAL);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 3);
$fmt->format(1234567.891234567890000);
If You use mysql then Use FORMAT function
like SELECT FORMAT(4.5,4)
also str_pad(4.5,STR_PAD_RIGHT)
for more http://php.net/manual/zh/function.str-pad.php
This question already has answers here:
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
(7 answers)
Closed 8 years ago.
Today i started writing a small PHP code and it left me confusing and so i parked here.
<?php
echo (int) ((0.5 + 0.3) * 10); // Outputs 8 as expected
<?php
echo (int) ((0.1 + 0.7) * 10); // Outputs 7 . How ????
Can someone answer with a detailed explanation ?
It is because floating point representation in computers is not exact for some numbers. As already said in the comments, 0.7 is represented internally as 0.699999 or so.
There are two websites that continuously pop up in these kind of questions:
http://floating-point-gui.de/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I prefer the first one, as it is a little lighter on the academics. Read that information and you'll understand.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an easy way to convert a number to a word in PHP?
Recently I needed to change a number to string just like below in php
$str=100;
{ Here will be the code which will return One Hundred not 100}
so $str=150 then will return one hundred fifty
How can I do it. How can I change the number to text in php (NB: number will be random)
please help me
Use Numbers_Words Pear package.
Example.
require('Numbers/Words.php');
$number = 150;
$nw = new Numbers_Words();
$numw = $nw->toWords($number);
echo $numw; //one hundred fifty
The solution to this problem has been suggested in the comments of the php documentation at the following location: http://www.php.net/manual/en/function.number-format.php#97020
Hope that helps
There is a PEAR package that will do this for you ...
http://pear.php.net/package-info.php?package=Numbers_Words
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Basically, I need to generate a list of all seven-digit numbers (for example, 1461979) from 0000000 to 9999999. I know there are 10^7 of them (ten million), but I can't think of an efficient function to output them. I'm sure there's a generic function out there that can do it - preferably in PHP, but I can port it if need be.
Loop from 0 to 9999999 and use one of the dozens of methods for zero-filling a number, such as printf("%07d", $val).
for ($i=0; $i<=9999999; $i++) {
echo sprintf("%07d", $i) . '<br / >';
}
This might break your browser though ;)