How to format number = "$lowprice" to currency - php

How to format number = "$lowprice" to currency
$price_content .= '<span>';
if ($currency_sympol) $price_content .= get_post_meta($id,$wprs_prefix.'lowprice', TRUE);
$price_content .= '<sup>'.$currency_sympol.'</sup>';
$price_content .= '</span>';

To format a number as currency use the money_format() function. (Docs)
$lowprice = 1234.56;
// Italian national format with 2 decimals
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $lowprice) . "\n";
// Eu 1.234,56
Since the currency is derived from the server locale, you might need to change it (see sample).

Related

correctly use of number_format function to show converted values correctly to colombian peso

calculate final price with discount and show converted their respective value in colombian pesos and dollars and use of function number_format
the proccess of dollar was successfully but arent correct the colombian peso showing value but before are converted from dollars to colombian peso (the calculated discount value are correct and showing correctly but not the final price showing correctly)
<!-- language: lang-php-->
//COUPON_TYPE = PERCENT_AMOUNT (%)
if($row1['coupon_type'] == 'percent_value'){
echo"DCTO = ". $DiscountedValueCOP = number_format($row['p_current_price'] * $row['ValorDolarUS'] * $row1['coupon_discount'] / 100);
$FinalPriceValueCOP = $DiscountedValueCOP - number_format($row['p_current_price'] * $row['ValorDolarUS']);
//FINAL PRICE MESSAGE FOR COLOMBIAN PESO...
echo "Final Price Of Product" . "<br>" . "With Discount Applied: " . "<br><br>" . "$" . number_format($FinalPriceValueCOP) . "<span> <i class='flag-icon flag-icon-col'> </i> COP</span>" . "<br><br>";
}else{
}
<!-- end snippet -->
I need this result please: (current price) = $18.272 COP (discount) = $3.753 COP = (final price) = $14.519 construction uniform
actually have this exactly value = $ - 15
Set the number of decimal digits to 3 its by default 0 as second param number_format as follows:
$DiscountedValueCOP = number_format($row['p_current_price']* $row['ValorDolarUS']*$row1['coupon_discount']/100,3);
//Final price should be current price - discount
$FinalPriceValueCOP = number_format($row['p_current_price']* $row['ValorDolarUS'],3) - $DiscountedValueCOP;
more about number_format
number_format($FinalPriceValueCOP,3)
please see this image here about the reazon of why minus symbol appear

How to add commas and round decimals by checking value

I'm building an personal app where I can follow my cryptocurrency coins which I'm currently holding and following. The problem I've noticed are the decimals behind the price. A currency normally has max 2 decimals, but the API of Coinmarketcap gives me more depending on how much the price is.
Below is an example of the value I get from the API, and how I actually want the price to be shown. Values above 1000 will get a comma and no decimals.
$950194.0 -> $950,194
$81851.6 -> $81,852
$4364.97 -> $4,365
$326.024 -> $326.02
$35.0208 -> $35.02
$4.50548 -> $4.51
$0.0547128 -> $0.0547128
I've never tried something ever like this before, so I really don't know how to start. Tried using round() and numberFormat(), but couldn't make the same like how I wanted above in the example.
You can use money_format to make thing easier. However, the problem is about what precision do you want. You have to figure it out yourself since I cannot find the pattern from your examples. Yet I wrote the simple function for you with round and money_format. What the rest is adjust the precision to the point where you want in each case.
<?php
function my_money_format($number, $precision=0)
{
$number = preg_replace( '/[^0-9.,]/', '', $number); // clean the input
$number = round($number, $precision);
$format = '%.' . $precision . 'n';
setlocale(LC_MONETARY, 'en_US'); // set money format to US to use $
return money_format($format, $number);
}
echo my_money_format('$950194.0', 0); // $950,194
echo "\n";
echo my_money_format('$81851.6', 0); // $81,852
echo "\n";
echo my_money_format('$4364.97', 0); // $4,365
echo "\n";
echo my_money_format('$326.024', 2); // $326.02
echo "\n";
echo my_money_format('$35.0208', 2); // $35.02
echo "\n";
echo my_money_format('$4.50548', 2); // $4.51
echo "\n";
echo my_money_format('$0.0547128', 7); // $0.0547128
echo "\n";

How to split long numbers by commas to readable number using php?

Am fetching a price from my database. It is being displayed using.
It display the prices as follows: 5000000
How can i split it to display something like 5,000,000
have you tried the manual?
number_format does exactly what you want:
<?php
$number = 5000000;
echo number_format($number); //output: 5,000,000
use money_format(string,number)
(Money format not fit for windows)
money_format
also use `number_format()
number_format — Format a number with grouped thousands
<?php echo number_format("5000000")."<br>"; ?>
number_format
You need to use the php built in function number_format for this
echo number_format($price);
<?php
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56
// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56
// Using a negative number
$number = -1234.5672;
// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($ 1,234.57)
// Similar format as above, adding the use of 2 digits of right
// precision and '*' as a fill character
echo money_format('%=*(#10.2n', $number) . "\n";
// ($********1,234.57)
// Let's justify to the left, with 14 positions of width, 8 digits of
// left precision, 2 of right precision, withouth grouping character
// and using the international format for the de_DE locale.
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
// Eu 1234,56****
// Let's add some blurb before and after the conversion specification
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
// The final value is GBP 1,234.56 (after a 10% discount)
?>

PHP adding country code for all currency formatting

I would like to have the country code display for all currencies when they are formatted. At the moment I have:
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(-10, 'USD') . PHP_EOL; //-$10.00
echo $formatter->formatCurrency(-10, 'CAD') . PHP_EOL; //-CA$10.00
How can I make the USD currency display either -US$10,00 or -USD$10.00?
Thanks

PHP JSON Format Currency Comma

I'm new to PHP.
My code reads a price value from a Steam game's json data.
http://store.steampowered.com/api/appdetails/?appids=8870
Problem is that the value of the price node is not formatted with a comma separator for dollars and cents. My code works to piece together the dollars and cents but is it the right way to do it for this instance. Also if there is another easier method of doing my newbie code, feel free to show me where it can be improved. Thanks!
<?php
$appid = '8870';
$ht = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($ht);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gname = $gdata['name'];
$gprice = $gdata['price_overview']['final'];
$gdesc = $gdata['detailed_description'];
$gusd = substr($gprice, 0, -2);
$gcent = substr($gprice, 2);
echo $gname. '<br>';
echo 'Price: $' .$gusd. ',' .$gcent;
?>
If I may ask another question... can the price data aka $gprice be added to another price data that is fetched, to return a total.
I would essentially do what you are doing except its much simpler to just divide by 100:
Turn the price into a float:
$gprice = $gprice / 100;
and then use money_format
Ref: PHP Docs - money_format
You could also do this, but there isn't really a need.
$gprice = (int) $gdata['price_overview']['final'];
The conversion is not bad, but you could also use this:
$gusd = $gprice/100;
echo $gname. '<br>';
echo 'Price: $' .str_replace('.', ',', $gusd);
or use money_format instead of replace, but it's a little more complicated.
also, to add another you could do it with just the + or += operators like this:
$gprice+= $gdata['price_overview']['final'];

Categories