Currency - Number formatting

Hi, clever people!

Having an absolute nightmare trying to figure out how to format a number with Revolution Slider.
I have an unformatted price coming from properties within property hive and need to format it.
The slider will only return the unformatted price. I have learned that with a little JQuery targeting a div containing the price, it could be possible to format the number into UK currently format. But I just cant work it out.

It would be great if anyone has an idea or even a solution. I’ve suddenly gone bald by way of pulling my hair out!

Here’s what I’ve got so far, but no joy…

Html: <span class="revSliderPrice">%meta:_price%</span>

Here is the code under JS section using EVENT:

jQuery(function ($){

  revapi39.bind("revolution.slide.onchange",function (e,data) {
         var priceAmt = $(".revSliderPrice").html();
  $(".revSliderPrice").text('$' + parseFloat(priceAmt, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "ÂŁ1,").toString());

});

});

…or maybe I could forget the revslider part and just ask, is there a way using JS to target a span/div class and format the number within that to currency decimals?
Is there a way that could be done?

using toLocalString is a good way to do it.

function toUSDCurrency(number) {
  return number.toLocaleString("en-US", {
    style: "currency",
    currency: "USD"
  });
}

Here’s a working example: https://jsfiddle.net/pmw57/d6bcb1u0/

1 Like

Great! thanks so much, I’ll give it a whirl.

So I guess I can just change the currency to UK “GBP”?

Yes. Update the function name, the locale, and the currency.

1 Like

Phenomenal! It works! Even with a meta tag as the price! Perfect! you sir are a gentleman!

Is there a way to remove the pennies/cents from the formatting? I’m probably asking too much and looking a gift horse in the mouth! :wink:

Yes there is. If you look at the toLocaleString documentation page you’ll see that there is a maximumFractionDigits option that you can use.

1 Like

Perfect, working as required!
Thank you so much, Paul.
This has been slowly killing me for days!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.