PHP check number have no decimals than add two decimal number [duplicate] - php

This question already has answers here:
PHP, Format Number to have always have 2 decimal places
(2 answers)
Closed 5 years ago.
Input:
(1) 2
(2) 2.3
PHP:
<?php if(is_int($ledger['Amount'])){$Amount = $ledger['Amount'].'.00';}else{$Amount = $ledger['Amount'];};?>
Output:
(1) 2.00
(2) 2.30
Anyone can please help me How can I get my expected output? Sorry for weak English and please help me for improving quality of this question.

Do not invent the wheel:
echo number_format(2, 2); // 2.00
echo number_format(2.3, 2); // 2.30

Related

How to set a decimal value up to 8 decimal places in php [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 3 years ago.
I want to show amount of bitcoins up to 8 decimal points. I have stored the value in Satoshi but when I try to convert the satoshi to BTC by dividing the satoshi value with 100000000 I am getting invalid result. here is my code.
$res = 3535/100000000;
echo $res;
the output of the above code is 3.535E-5 isntead 0.00003535.
please tell me the proper solution.
echo number_format(3535/100000000, 8);
Where 8 is the count of 0's. For example for 3535/1000000000 you need to write
echo number_format(3535.0/1000000000, 9);

Php change 5.0E-5 into 0.00000050 [duplicate]

This question already has answers here:
Display float value w/o scientific notation
(4 answers)
Closed 5 years ago.
I am working on my site about bitcoins. And people can have balances there. I am btw working in php. In my $user_data["balance"] the value is 50. And I want that to get converted by php into 0.00000050. So I tried this:
$balance = $user_data["balance"] / 1000000;
But what I get from it, is this: 5.0E-5. How to change it into 0.00000050?
I would be really thankfull if somebody would help me!
Use number_format to get results with 8 decimals.
Example-
$balance = number_format($user_data["balance"] / 1000000,8,".","");
echo $balance; //output will 0.00005000
But if you want 0.00000050 then divide it by 100000000
Example
$balance = number_format($user_data["balance"] / 100000000,8,".","");
echo $balance; //output will 0.00000050

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

PHP format for Lakh and Crore [duplicate]

This question already has answers here:
Is there any PHP function to convert number to currency with thousands separators?
(2 answers)
Closed 7 years ago.
Want to format number in php like 1,00,00,00,000. I am trying the following function
number_format($var['count'])
but it is giving me following answer 1,000,000,000
Thanks and Regards
You can try this, to do additional formatting read through PHP's documentation: PHP.NET - Money Format
$dollar = 6000000000;
setlocale(LC_MONETARY, 'en_IN');
echo $dollar = money_format('%!i', $dollar);
Output:
6,00,00,00,000.00
Try this
$number = 123554646;
setlocale(LC_MONETARY,"en_IN");
echo money_format("The price is %i", $number);
Output
The price is INR 12,35,54,646.00
Check Phpfiddle Preview

shorten a long number with php [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 9 years ago.
how can i shorten a long number with php ? So a number like 0.3483748937847832 would become 0.34
Please search stackoverflow first - from PHP: show a number to 2 decimal places
number_format()
return number_format((float)$number, 2, '.', '');
https://stackoverflow.com/a/4483561/689579
or
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00
https://stackoverflow.com/a/4483715/689579
Use round
echo round($number,2);

Categories