I'm have this number
55550
But I it should be 555.50, I tried number_format() and sprinft but did not work.
<?php
$num = 55550;
$num = (float) $num / 100;
echo number_format($num, 2, ".", "");
by casting the number to float, and dividing by 100 you can use number_format to change the format.
After a few calculations I get:
$int = 14.285714285714;
How can I take only the first four digits? Expected output: 14.28
Doing this with string functions is absolutely the wrong way to go about this, and these nearly identical answers look pretty spammy. If you want to round a number, round a number!
$int = round(14.285714285714, 2);
To truncate (as opposed to rounding), floor is the correct function in PHP:
$int = floor(14.285714285714 * 100) / 100;
Both work without any type conversions or casting.
Also note that a number with decimal places is categorically not an integer.
use substr function
$int = 14.285714285714;
echo substr($int, 0, 5);
$newint = (float)substr($int, 0, 5);
IF you want to round the number you can use
round($int, 2);
OUTPUT WILL BE : 14.29
LINK HOW TO ROUND
Try number format,
string number_format ( float $number [, int $decimals = 0 ] )
so
$int = 14.285714285714;
$small = (float)number_format ( $int ,2,'.', '' ); //gets rid of the "," for thousand separator
http://us2.php.net/manual/en/function.number-format.php
floor(100 * 14.285714285714) / 100
14.28
I need to know how to take
10.25 and turn it to 1025
basically it needs to remove full stop from any number for e.g.
1500.25 it should be 150025
$number = str_replace('.','',$number);
if the currency is a float: multiply with 100 (and cast the result to int).
$currency = 10.25;
$number = (int)($currency * 100); //1025
note that this solution will only get the first two decimals saved - if you have a number like 10.123, the 3 will simply be cut off without rounding.
Floating-point arithmetic is by it's definition not exact. Therefore it's worthwhile to NOT cast the value to a float if it's a string, and avoid casting it into a string if it's a float.
Here's a function which takes care to check the value type:
function toCents($value) {
// Strings with a dot is specially handled
// so they won't be converted to float
if (is_string($value) && strpos($value, '.') !== false) {
list($integer, $decimals) = explode('.', $value);
$decimals = (int) substr($decimals . '00', 0, 2);
return ((int) $integer) * 100 + $decimals;
// float values are rounded to avoid errors when a value
// like ".10" is saved as ".099"
} elseif (is_float($value) {
return round($value * 100);
// Other values are strings or integers, which are cast
// to int and multiplied directly.
} else {
return ((int) $value) * 100;
}
}
if you want replace one character only, use strtr instead str_replace
$number = str_replace('.','',$number);
and
$number = strtr($number, array('.', ''));
same output but strtr is better.
I have this string :
000000000000100
and need to convert it to:
1,00
So, the rules are:
Divide the number by 100 and use a comma as decimal separator
Strip leading zeros
Keep two decimals
From the PHP Manual page on number_format:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
If you want numbers like 123456 be formatted as 1234,45, use:
echo number_format($number / 100, 2, ",", "");
If you need a dot as thousands separator (1.234,56):
echo number_format($number / 100, 2, ",", ".");
The zeros are automatically removed by PHP when converting the string to a number.
string number_format ( float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
Manual: http://php.net/manual/en/function.number-format.php
// divide by 100 to shift ones place.
echo number_format((int)'000000000000100' / 100,2,',','');
I have a price "0,10" or "00000,10"
Now when i try
number_format($price, 2, ',', '')
I get 0,00.
How can i fix this? I want 0,10 $.
I don't want rounding.
Or when i have 5,678, i get 5,68. But i want 5,67.
Several people have mentioned rounding it to 3 and then dropping the last character. This actually does not work. Say you have 2.9999 and round it to 3 it's 3.000.
This is still not accurate, the best solution is this:
$price = '5.678';
$dec = 2;
$price = number_format(floor($price*pow(10,$dec))/pow(10,$dec),$dec);
What this does is takes the price and multiplies it by 100 (10^decimal) which gives 567.8, then we use floor to get it to 567, and then we divide it back by 100 to get 5.67
You can increase the size of the number before rounding down with floor:
$price = floor($price * 100) / 100;
$formatted = number_format($price, 2, ',', '');
Another solution, which may give better precision since it avoids floating-point arithmetic, is to format it with three decimals and throw away the last digit after formatting:
$formatted = substr(number_format($price, 3, ',', ''), 0, -1);
you should convert comma-filled number back to normal decimal before with str_replace.
$number = str_replace(",", ".", $number);
and then you can use number_format
"00000,10" is a string. You should a decimal point. To get the desired behaviour, you could use:
echo substr(number_format(str_replace(',', '.', $price), 3, ',', ''), 0, -1);
Use this (needs activated intl PHP extension)
$numberFmtCurrency = new NumberFormatter('de_AT', NumberFormatter::CURRENCY);
$numberFmtCurrency->setAttribute(NumberFormatter::ROUNDING_INCREMENT, 0);
$numberFmtCurrency->formatCurrency(328.13, 'EUR'); // prints € 328.13 (and not 328.15)
If you are literally just wanting to clear leading zeroes and just limit the length, rather than round to a certain amount of decimal places, a more generalised solution could be this function:
function cutafter($string,$cutpoint,$length)
{
$temp = explode($cutpoint,$string);
$int = $temp[0];
$sub = $temp[1];
return number_format($int,0).','.substr($sub,0,$length);
}
Example:
$number = "005,678";
$answer = cutafter($number,",",2);
$answer now equals "5,67"
Just before number_format is executed the string "0,10" is converted by php to an number. because php always uses the engish notation the it won't look after the comma.
echo "4 apples" + 2;
output: 6
The " apples" part is ignored just as your ",10" is ignored.
Converting the "," to a "." allows php to see the other digits.
$price = str_replace(',', '.', '0,10');
number_format($price, 2, ',', '');
My problem was that html validator error messege thar number_format() argument is not double.
I solved this error message by placing floatval for that argument like number_format(floatval($var),2,'.',' ') and that is working good.
function format_numeric($value) {
if (is_numeric($value)) { // is number
if (strstr($value, ".")) { // is decimal
$tmp = explode(".", $value);
$int = empty($tmp[0]) ? '0' : $tmp[0];
$dec = $tmp[1];
$value = number_format($int, 0) . "." . $dec;
return $value;
}
$value = number_format($value);
return $value;
}
return $value; // is string
}
Unit Testing:
Passed / 1100000 => 1,100,000
Passed / ".9987" => .9987
Passed / 1100.22 => 1,100.22
Passed / 0.9987 => 0.9987
Passed / .9987 => 0.9987
Passed / 11 => 11
Passed / 11.1 => 11.1
Passed / 11.1111 => 11.1111
Passed / "abc" => "abc"
See this answer for more details.
function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
$number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
return number_format($number, $decimals, $decPoint, $thousandsSep);
}