I have this question:
How can round two decimals place to next multiple to ten? Follow this example:
$number = 120.37
// I would like some functions or trick to transform this into 120.40
Is it possible in php?
use this number format
number_format($number, 2, '.', '');
It will give output 120.40
try this
number_format(round($number,1), 2, '.', '');
or just
round($number,1);
if you not need it with 2 decimals
Try this
round($number * 10) / 10;
Related
I need transform 100 to 1.00, 345 to 3.45 or any number with 3 digits or more to record in db like a decimal.
Don't need add .00, just transform last two numbers in decimal.
I try number_format($num, 2) but is wrong.
It seems, that you want to divide your numbers to 100, so using number_format() with the appropriate $decimal_separator and $thousands_separator is an option:
<?php
echo number_format($num / 100, 2, '.', '');
?>
Since you are converting from an integer to a float, you can achieve this simply by dividing the number by 100.
$input = 100;
$value = floatval($input) / 100;
$value = number_format($value, 2);
echo $value;
You may then use number_format (as you were using before) to force two decimal places after any evenly divided float numbers (such as 100).
Demo: PHP Sandbox Example
My number is 495 plain and simple.
I'm trying to format this number to have it display $4.95
what I've done is number_format($number, 2, '.', '')
but that outputs 495.00
I know it can't be this difficult. Anyone familiar with number_format?
I just need it to add a decimal two numbers from the right so the cents are always displayed. ex. 4.95, 12.58, 23.39... You get the idea.
Thank you.
If you're working with integers instead of floats, you'd first have to divide by 100.
$number = 495;
echo number_format($number/100, 2, '.', '')
Assuming your numbers are all whole numbers you can do the following:
$mynum = 495;
function formatMyNum($num) {
return $num/100;
}
Number format just displays floats using N number of decimals and the decimal/thousand separator characters you choose.
If you passed 4.95 to it, it would show fine so to do that just divide your $number by 100 before passing it to the function.
echo number_format($number/100, 2, '.', '');
I have been trying all morning but how would I this number 1304583496 to look like this
13.04583496
and the same goes for this number
456604223 to 4.56604223
There are always 8 numbers to the right.
Divide it with 100000000, or use "pow" function:
$number / pow(10, 8);
You could also use the official number_format method after division to keep your ending zeroes and have your number displayed in a nice manner.
<?php
$num = 1304583496; //the number
echo number_format($num/100000000,8,"."," "); //number of decimals = 8, comma seperator is . and thousands seperator is a space here
?>
For more information on this function: http://www.w3schools.com/php/func_string_number_format.asp
I have column named Profit/Loss(USD) , this column contains numbers which are defined by a set of calculations and the variable for this column. The problem is that some values come out like 7.0E-6 . How do i adjust all to 2 decimal places? all answers appreciated!
Use the in built function number_format like so:
number_format($number, 2);
If you don't want to add thousand seperators, you can simply skip it:
number_format($number, 2, '.', '');
// ^ No thousand seperator
you can use the function number_format to format your number
$formatted_number = number_format($number, 2);
You can use the printf family of functions, for example:
echo '<td>'.sprintf('%.2f', $number).'</td>';
The documentation lists all the formatting codes you can use.
I want to display a number as a winning percentage, similar to what you would see on ESPN baseball standings. If the user has no losses, I would like the percentage to read 1.000. If the user has no wins, I would like it to read .000. If the user has a mix of wins and losses, I would like to display .xyz, even if y or y and z are 0's.
This code gets me no trailing 0's, and also a 0 before the decimal (0.4 instead of .400, 0.56 instead of .560):
$wpct1 = $wins / ($wins + $losses);
if($wpct1 == 1){$wpct = '1.000';}else{$wpct = round($wpct, 3);}
This code gets the initial 0 befoer the decimal out of there, but still no trailing zeroes (.4 instead of .400):
$wpct1 = $wins / ($wins + $losses);
if($wpct1 == 1){$wpct = '1.000';}else{$wpct = substr(round($wpct, 3), 1, 4);}
This second solution is getting me closer to where I want to be, how would I go about adding the trailing 0's with an additional piece of code (one or two trailers, depending on the decimal), or is there another way to use round/substr that will do it automatically?
$wpct = ltrim(number_format($wins / ($wins + $losses), 3), '0');
This formats the number the three digit after the decimal point and removes any leading zeroes.
See number_format and ltrim for further reference.
sprintf('%04d',$wpct1);
Will print leading zeros
You need
number_format($wpct, 3)
You could use str_pad() to add trailing zeros like this:
if($wpct1 == 1){$wpct = '1.000';}else{$wpct = str_pad(substr(round($wpct, 3), 1, 4), 3, '0');}
sounds like number_format is what you're looking for.
Have a look at sprintf()
$x = 0.4;
echo sprintf("%01.3f", $x);
You can use php's built in number_format