How do you change the currency format in html?

The number presented as monetary value is readable and eye-catching. If the number has more than three digits, it should be displayed differently. For instance, when you write 1000 is not as clear and nice as $1.000. In this case, the currency is USD. However, different countries have other conventions for displaying values.

The Intl.NumberFormat object is a constructor for objects enabling language-sensitive number formatting. This method is considered as the modern and fast way for formatting numbers.

const formatter = new Intl.NumberFormat('en-US', {
  style: 'valuta',
  valuta: 'USD',
  minimumFractionDigits: 2
})
formatter.format(2000) // "$2,000.00"
formatter.format(20) // "$20.00"
formatter.format(215241000) // "$215,241,000.00"

Another fast solution is compatible with every major browser:

How do you change the currency format in html?
Javascript format number

const profit = 1234.5678; console.log(profit.toFixed(4)); //returns 1234.5678 (rounds up) console.log(profit.toFixed(2)); //returns 1234.57 console.log(profit.toFixed(6)); //returns 1234.567800 (pads the decimals)

All you need is to add the currency symbol and get the amount in the specified currency such as:

Another solution is toLocaleString, which offers the same functionality as Intl.NumberFormat, but toLocaleString in pre-Intl does not support locales, and it uses the system locale.

How do you change the currency format in html?
Javascript format number

let val = (2500).toLocaleString('en-US', { valute: 'USD', }); console.log(val);

Parameters are locales and options. The locales are made up of language code and the country code, such as en = English (language). There are lots of options, and one of them is style. The style is for number formatting. The three values are:

  • decimal (plain number formatting)
  • currency (currency formatting)
  • percent (percent formatting)

Choosing a different locale and currency will format the monetary value accordingly.

Well, this doesn't fit your own use case unfortunately, and it's fairly obvious, but you could do this for integer numbers from -999 to 999.

.currency:before{ content:'$'; }
.currency:after{ content: '.00'; }
<span class="currency">789</span>

Then, a possible, annoying, solution if you're working server side, is to convert your number to a reversed string and loop over each character. If you really wanted you could place the characters into li's and css could then do the formatting you wanted as follows. However this is incredibly pointless as you may as well simply author the string at that point.

.currency li:before{ content: ' ,';}
.currency li:first-child:before{ content:'$' !important; }
.currency *{ display: inline-block; }
.currency, .currency > *{ display: inline-block; }
.currency:after{ content: '.00'; }
<ul class="currency"><li>123</li><li>456</li><li>789</li></ul> 
<ul class="currency"><li>456</li><li>789</li></ul> 
<ul class="currency"><li>789</li></ul> 

For an even deeper dive into pointless effort, you could prepend a

<style> .currency:after{ content: '.00'; } </style>

above the <ul>, allowing your script to change the decimal value in CSS, lol

Still, if you were to cave and use JavaScript, then the CSS may actually be somewhat useful. You can output a plain int or double (any precision) and just have JS break it into <li>s.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a number and the task is to format the number of an HTML element as currency using CSS and a little bit of JavaScript. It is not possible to complete the task with pure CSS. A bit of JavaScript is needed for the parsing of the number to add the commas.

    Approach: In the code, the CSS class currSign adds the currency sign (like ‘$’) before the number. The JavaScript function toLocaleString() returns a string with a language-sensitive representation of the number.

    Example:

    <!DOCTYPE html>

    <html>

    <head>

        <meta name="viewport" content=

            "width=device-width, initial-scale=1">

        <style>

            .currSign:before {

                content: '$';

            }

        </style>

    </head>

    <body>

        <div class="myDIV">88123.45</div>

        <div class="myDIV">75123.45</div>

        <div class="myDIV">789415123.45</div>

        <div class="myDIV">123</div>

        <script>

            let x = document.querySelectorAll(".myDIV");

            for (let i = 0, len = x.length; i < len; i++) {

                let num = Number(x[i].innerHTML)

                          .toLocaleString('en');

                x[i].innerHTML = num;

                x[i].classList.add("currSign");

            }

        </script>

    </body>

    </html>

    Output:

    How do you change the currency format in html?


    How do I change the format of a currency?

    On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.

    How do you create a currency field in HTML?

    <div class="main">.
    <h1>Auto Formatting Currency</h1>.
    <form method="post" action="#">.
    <label for="currency-field">Enter Amount</label>.
    <input type="text" name="currency-field" id="currency-field" pattern="^\$\d{1,3}(,\d{3})*(\.\d+)?$" ... .
    <button type="submit">Submit</button>.
    </form>.

    How do I change the currency format in numbers?

    Select the cells or table you want to format. In the Format sidebar, click the Cell tab, then click the Data Format pop-up menu and choose Currency. Do any of the following: Set the number of decimal places: In the Decimals field, type the number of decimal places you want to display.

    How do I convert currency to string?

    string value = (Convert. ToDecimal(ReturnValue)). ToString("c");