How to convert a number to a specific format in PHP? - php

I would like to show a number 1000000 in the format 10,00,000 in PHP.
What is the PHP function to do this conversion?

use number_format function
<?php
echo number_format(100000); // prints 1,000,000
?>

If you need to display a value formatted as Lakhs and Crores:
$amount = 1000000000;
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
should give 100,00,00,000 instead of 1,000,000,000

Related

Convert 8199000000 to decimal (php)

The API I fetch my data with, returns me 8199000000 for the price.
Which should be 81.99 but I should also expect something like 15499000000 which should result into 154.99.
How can I parse that clean and nice?
You can just divide by 1e8 (1 and 8 zeroes).
$price = 8199000000;
$real = $price / 1e8;
echo $real, PHP_EOL; // 81.99
$price = 15499000000;
$real = $price / 1e8;
echo $real, PHP_EOL; // 154.99
Nevermind I should have just used a regex.....
^([0-9]{2}|[0-9]{3})([0-9]{2})0

Number formatting (Money) in PHP

I'm trying to format numbers with not decimal points or commas, I've tried number_format() and money_format() and can't seem to get the result I need.
number_format($item->amount,2)
Result: 14,995.00
money_format("%i", $item->amount)
Result: 14,995.00
I'm want to get the following numbers formatted correct.
14995 needs to be £149.95
6795 needs to be £67.95
What is the best way to get the result above?
Using brick/money (disclaimer: I'm the author):
use Brick\Money\Money;
// Instantiating from a decimal amount
$money = Money::of('67.95', 'GBP');
echo $money->formatTo('en_GB'); // £67.95
// Instantiating from a minor amount (cents)
$money = Money::ofMinor(6795, 'GBP');
echo $money->formatTo('en_GB'); // £67.95
setlocale(LC_MONETARY, 'en_GB.UTF-8'); // change if needed
echo money_format('%n', 6795 / 100);
returns
£67.95
for me, so that should work. As i mentioned in my comment, you probably can't get the code to both display 14995 as 1,499.50 (divide by 10) and 6795 as 67.95 (divide by 100), so I'd recommend refactoring the code accordingly.

PHP shopping cart calculation

Hi I need to remove 10% from a shopping carts subtotal
Original code:
<?php echo number_format($order->subtotal,2);?>&OID=<?php echo $order->trans_id;?>
I know it's not precise, but would something like this work?
<?php echo number_format($order->subtotal * 0.909090909,2);?>&OID=<?php echo $order->trans_id;?>
Thanks
Use sprintf()
$a = 2324.56*0.909090909 ;
echo sprintf('%0.2f',$a);
output // 2113.24
sprintf() will handle the floating point precession which is the best way to handle.
If needs to display the money format for specific locale it could be doing using money_format
$a = 2324.56*0.909090909 ;
$amount = sprintf('%0.2f',$a);
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#1n', $amount) . "\n";
output // $2,113.24
Here is an explanation on number_format() -ve value precession issue
http://www.howtoforge.com/php_number_format_and_a_problem_with_negative_values_rounded_to_zero
that probably would work, but why not just subtract the 10 percent? If that's the goal, why not just do it? keep in mind number_format rounds up, but I expect that's desired.
<?php echo number_format( ($order->subtotal - ($order->subtotal* .1) ) ,2);?>
This is the math that actually subtracts 10% why not use this instead of something that's close?
try this
$subtotal = $order->subtotal;
$cut_subtotal = $subtotal *(10/100);
$subtotal_new = $subtotal-$cut_subtotal;
Now use this in your code
<?php echo number_format($subtotal_new,2);?>

Formatting numbers from 1000 to 10.00

I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().

Pricing issues with pounds and pence!

At the moment I store prices for products in the database as a pence number. So 4321 in the database means £43.21.
Then when reading it out, I divide by 100 to get it in pound and pence format.
However, I have a problem.
If the price is 4320, the returned value is 43.2 without the 0.
How can I get around this?
Thanks!
You can format strings with sprintf
See example 9:
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
<?
echo money_format("%i", 1234.5)
//Output: 1234.50
?>
You can use money_format.
money_format() should do the trick. Alternatively number_format() or the powerful printf().
echo number_format($float, 2, '.', '');
and for pretty printing of large values:
echo number_format($float, 2, '.', ',');

Categories