Formatting a number as currency using CSS

前端 未结 4 949
北海茫月
北海茫月 2020-12-15 15:32

Just wondering if anyone knows whether it is possible to format the content of an element as currency using only CSS. It would be nice to have how the value is presented in

4条回答
  •  执念已碎
    2020-12-15 15:45

    The currency format can be achieved with CSS and a bit of Javascript which is needed for the parsing of the number to add the commas. A CSS class adds the additional styling like negative (red) or currency sign (i.e. $ Dollar sign). The approach is a follows:

    1) Convert the value to number (adds the commas based on the locale)

    Number(value).toLocaleString('en');
    

    2) Add a class to determine if it is negative or positive value (i.e. red color)

    .enMoney::before {
        content:"$";
    }
    .negMoney {
        color:red;
    }
    

    See more detail here with the sample code and css:

    http://www.ozkary.com/2014/04/format-currency-with-javascript-and-css.html

提交回复
热议问题