PHP change 0.1 to 0.10 - php

Is there a PHP function that will make the number always have 2 decimals places, even if it's 0?

No, but you can format a number to a string with decimals
number_format — Format a number with grouped thousands
Example:
echo number_format(0, 2); // 0.00
EDIT: the printf/sprintf solutions suggested deserve some upvotes too

Do you mean you want to print additional digits even if there's only one digit after the decimal? You can do something like:
$x = 0.1;
printf("%.2f", $x);

Related

Set max and min value of variable and limit decimals

I have a variable $percentchance that represent the percent chance to succeed with something. What I would like help with is, for it to never display above 100 or below 0.
Currently I have used this min function to never go above 100.
<?php echo min(100, $percentchance); ?>
Another issue is that sometimes I get the value of percent like 26.3456, and I wish for some way to make it round up or round down and display only 2 decimals like 26.35 in that case.
You have to set an upper and lower bound for the first problem. The second problem can be tackled with number_format.
Example:
echo number_format(min(100, max(0, $x)), 2);
To enforce always rounding up to the next second digit (e.g. 1.111 would be rounded to 1.12), you could utilise ceil.
echo number_format(min(100, max(0, ceil($x*100)/100)), 2);
if you need to round up with just two decimal use round()
echo round(26.3456, 2);

Limiting remainder in php

I have a code in which calculates the Effective interest rate.
when I echo it I get 5.1161897881733 which I want to limit the remainder and output it like 5.11, is there any functions use to limit the remainder in php?
A billion ways to do this. The task for you here is to pick one.
Method 1 - round()
This will just round your number. The exact rules can be found in the PHP.net documentation.
round($someNumber, 2);
Method 2 - floor()
Floor will round the number down
floor($someNumber, 2);
Method 3 - ceil()
Opposite to floor() this will round your number upwards.
ceil($someNumber, 2);
Method 4 - number_format()
This will format any number. number_format() has a gazillion possible inputs in which you can choose decimal characters etc.
// Will round your number to 2 decimals with a . as decimal character
number_format($someNumber, 2, ",", ".");
Feel free to edit and add more options :)
round($result, 0)
The 0 represents the decimal places.
http://php.net/manual/en/function.round.php

¨Showing significant figures

I have a question regarding number formating in PHP.
I have a variable called "average", which is simply an average of a few values. To make it clear I rounded the number to 2 decimal places. Now the problem is, that if the average is for example 2.90, it only shows 2.9. Is there any way of displaying 2 decimal places always? I though I could do it by multiplying the number by 100, rounding it to zero d.p. and then divide by 100 again, but that seems a bit overcomplicated if there is an easier way of doing it.
Maybe you can try the number_format(float $number [, int $decimals = 0 ])?
For more information, take a look at http://php.net/manual/en/function.number-format.php
Format the output with printf
printf("%.1f", $num); // prints 1 decimal place
printf("%.2f", $num); // prints 2 decimal places

PHP - Convert fixed number into decimal, using last 2 digits as the decimal places

I have a situation where all records in a CSV I'm parsing are currency, but the values are not separated by a decimal point. So for instance, value '1234' is actually '12.34', and '12345' is '123.45'.
I'm struggling to find a way to manually convert these values into decimals. I can't user number_format, because it will give me an output like so:
$original_num = 1234;
$formatted_num = number_format($original_num, '2', '.', '');
$formatted_num = 1234.00; //Output
The other issue is that sometimes I may have a value like '436257.5' after I combine two numbers, which is actually '436.2575' so I can't just manually push in a '.' two places from the end of the string. Should I consider formatting it differently while I'm parsing the file?
Assuming you're using integers to always represent decimals with 2 places of precision after the decimal point, you just divide with 100 to insert the dot in the right place.
What do you mean, "combine"? You mean multiply? You should renormalise after each multiplication, and never get into a situation where you're representing decimals of differing precisions (unless you keep track of the precision, which you can do but it's pain in the ass and normally unnecessary).
function multiply($a, $b) {
return round($a * $b / 100);
}
function format($a) {
return sprintf("%.2f", $a / 100);
}
Since number_format() function always adds 2 00 as decimal, you can divide the value by 100.
number_format($original_num/100,2);

getting an odd division error with PHP number_format()

my script calculates a number X by dividing two other numbers, A by B.
X=A/B
when i use number_format(A,2) on A before calculating X, i get a very odd number. Actual figures:
1,045.00 / 5 = 0.2
but if i don't use number_format on A before the division, i get the correct answer. Is number_format somehow making A into a non-number?
1045 / 5 = 209
number_format should be used only while pretty printing the number. Its return value should not used in calculation as you did.
Example:
If $A = 1045;
then number_format($A,2) will be 1,045.00 now if you treat 1,045.00 as a number it will be 1 as comma and remaining char will be ignored and 1/2 is 0.5 which you are getting.
You want round(A, 2), not number_format() which is for string representations (hence named "format").
The docs show that number_format returns a string. Have you tried casting the result of number_format() to a numeric type before your mathematical manipulation?
I had similar issues. It could be better if we use number format dec_point and thousand_separator parameters. you could use number_format($number, 2, '.', ''); It will help to remove your thousand separator
number_format makes it into a string with commas between thousands, and the comma will be confusing the divisor into thinking that's the decimels based on your locale.

Categories