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,',','');
Related
Previous Code
$product->selling_price
Result 1.1000
I tried
({{ ceil($product->selling_price) }})
1
I want 1.100 (for Kuwait dinar)
You can use number_format
number_format((float)$product->selling_price, 3, '.')
PHP official documentation
number_format ( float $num , int $decimals = 0 , string|null
$decimal_separator = "." , string|null $thousands_separator = "," ) :
string
Formats a number with grouped thousands and optionally decimal
digits.
Ref:https://www.php.net/manual/en/function.number-format.php
Try This One: I have solved using this:
number_format((float)$product->payable_amount, 2,)
I have a full int value already with the decimals and I want to convert like this in PHP
500 -> 5,00$
5070 -> 50,70$
50070 -> 500,70$
500000 -> 5.000,00$
so the format should be like this xxx.xxx.xxx.xxx,xx$
any function for that?
number_format() can do the job.
It works like this : string number_format(float $number, int $decimals = 0, string $dec_point = ".", string $thousands_sep = ",")
number
The number being formatted.
decimals
Sets the number of decimal points.
dec_point
Sets the separator for the decimal point.
thousands_sep
Sets the thousands separator.
In your case, that could be $myMoney = number_format($cents / 100, 2, ",", ".") . "$";
As I see you have your money format in cents so you can do:
$prices = [
500,
5070,
50070,
500000
];
foreach ($prices as $price) {
echo money_format("Price: %i", $price / 100);
}
I need to represent 2.5 into exact 2.50 using php. I have used
sprintf('%0.2f',2.5)
But it did not work. What can be the possible answer?
I have used many round off functions too like
round()
But I didn't get solution. Please help me.
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$number= "2.5";
echo number_format((float)$number, 2, '.', ''); // Outputs -> 2.50
This function returns a string.
You can use
string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
$foo = "2";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 2.00
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
The number is 13911392101301011 and regardless of using sprintf or number_format i get the same strange result.
sprintf('%017.0f', "13911392101301011"); // Result is 13911392101301012
number_format(13911392101301011, 0, '', ''); // Result is 13911392101301012
sprintf('%017.0f', "13911392101301013"); // Result is 13911392101301012
number_format(13911392101301013, 0, '', ''); // Result is 13911392101301012
As you actually have the number as a string, use the %s modifier:
sprintf('%s', "13911392101301011"); // 13911392101301011
Note that PHP is using a signed integer internally. The size depends on your system.
32bit system:
2^(32-1) = 2147483648
64bit system:
2^(64-1) = 9223372036854775808
-1 because 1 bit is reserved for the signage flag.
Since you are dealing with large numbers here, you may want to keep them as strings and perform numerical operation on the string values using BCMath functions.
$val = "13911392101301011";
echo $val; // 13911392101301011
echo bcadd($val, '4'); // 13911392101301015
echo bcmul($val, '2'); // 27822784202602022
You can do easily this way :-
ini_set("precision",25); // change 25 to whatever number you want or need
$num = 13911392101301011;
print $num;
Documentation states that $number in number_format is float so there is explicit typecast. Equivalent would look like this:
sprintf('%017.0f', (float) "13911392101301011");
Float is precise to around 14 digits and your number has 17 digits.
Your number_format call is setting the . and , to blank
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
try this:
number_format(13911392101301011, 0, '.', ',');