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,)
Related
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'm trying to fetch data from a database, using json. but the data retrieve from the database is of type String which is causing an error.
What I want to do is to format the data to a float type with 2 decimal places.
I have gone through similar probable answers, but none seems to solve the challenge.
This is what I have done.
$num1 = "9";
//Using number_format
echo (float) number_format($num1,2,'.',''); // result is 9 instead of 9.00
//Using Floatval
echo floatval("9"); // result is 9 instead of 9.00
How can I get the result formatted as 9.00 ?
You need to put the (float) inside the number_format.
echo number_format((float) $num, 2, '.','');
try this: echo number_format ((float) $num1, 2, '.', '');
the format of this is: string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) from http://php.net/manual/en/function.number-format.php
another method is: sprintf('%0.2f', $num1);
and sprintf is used to format strings and might be applicable in other areas as well. http://php.net/manual/en/function.sprintf.php
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
$numval = 12345.50;
Desired output:
12 345,50
The comma instead of a dot is not a problem but how can I get the thousands separator to be a white-space?
I noticed PHP money format with spaces but this is not a duplicate post. Using number_format is out of question as it rounds the input value. I can't allow the values passed through it to be rounded at all.
Is there a built-in way to do exactly what number_format() does, but without rounding the value or do I have to write my own function to do this?
If rounding is out of the question, so is float values. You must go back to integers if you don't want rounding since floating-point arithmetic is not exact. In that case you'll have to implement the formatting function yourself.
This is especially true if you are handling money. See for example Why not use Double or Float to represent currency?
This looks like the version of the function you want to use:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
So for example:
$newNumber = number_format($oldNumber, 2, ",", " ");
For more information check out http://php.net/manual/en/function.number-format.php
From this comment of the number_format() page (I modified the function to match the number_format defaults though).
To prevent rounding:
function fnumber_format($number, $decimals=0, $dec_point='.', $thousands_sep=',') {
if (($number * pow(10 , $decimals + 1) % 10 ) == 5) //if next not significant digit is 5
$number -= pow(10 , -($decimals+1));
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
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,',','');