Formatting numbers (decimal places, thousands separators, localization, etc) with CSS

CssNumber Formatting

Css Problem Overview


Is it possible to format numbers with CSS? That is: decimal places, decimal separator, thousands separator, etc.

Css Solutions


Solution 1 - Css

The CSS working group has publish a Draft on Content Formatting in 2008. But nothing new right now.

Solution 2 - Css

Unfortunately, it's not possible with CSS currently, but you can use Number.prototype.toLocaleString(). It can also format for other number formats, e.g. latin, arabic, etc.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Solution 3 - Css

Well, for any numbers in Javascript I use next one:

var a = "1222333444555666777888999";
a = a.replace(new RegExp("^(\\d{" + (a.length%3?a.length%3:0) + "})(\\d{3})", "g"), "$1 $2").replace(/(\d{3})+?/gi, "$1 ").trim();

and if you need to use any other separator as comma for example:

var sep = ",";
a = a.replace(/\s/g, sep);

or as a function:

function numberFormat(_number, _sep) {
    _number = typeof _number != "undefined" && _number > 0 ? _number : "";
    _number = _number.replace(new RegExp("^(\\d{" + (_number.length%3? _number.length%3:0) + "})(\\d{3})", "g"), "$1 $2").replace(/(\d{3})+?/gi, "$1 ").trim();
    if(typeof _sep != "undefined" && _sep != " ") {
        _number = _number.replace(/\s/g, _sep);
    }
    return _number;
}

Solution 4 - Css

Probably the best way to do so is combo of setting a span with a class denoting your formatting then use Jquery .each to do formatting on the spans when the DOM is loaded...

Solution 5 - Css

No, you have to use javascript once it's in the DOM or format it via your language server-side (PHP/ruby/python etc.)

Solution 6 - Css

Not an answer, but perhpas of interest. I did send a proposal to the CSS WG a few years ago. However, nothing has happened. If indeed they (and browser vendors) would see this as a genuine developer concern, perhaps the ball could start rolling?

Solution 7 - Css

If it helps...

I use the PHP function number_format() and the Narrow No-break Space ( ). It is often used as an unambiguous thousands separator.

echo number_format(200000, 0, "", " ");

Because IE8 has some problems to render the Narrow No-break Space, I changed it for a SPAN

echo "<span class='number'>".number_format(200000, 0, "", "<span></span>")."</span>";

.number SPAN{
	padding: 0 1px; 
}

Solution 8 - Css

I don't think you can. You could use number_format() if you're coding in PHP. And other programing languages have a function for formatting numbers too.

Solution 9 - Css

You cannot use CSS for this purpose. I recommend using JavaScript if it's applicable. Take a look at this for more information: https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format

Also As Petr mentioned you can handle it on server-side but it's totally depends on your scenario.

Solution 10 - Css

You could use Jstl tag Library for formatting for JSP Pages

JSP Page
//import the jstl lib
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

<c:set var="balance" value="120000.2309" />
<p>Formatted Number (1): <fmt:formatNumber value="${balance}" 
        type="currency"/></p>
<p>Formatted Number (2): <fmt:formatNumber type="number" 
        maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (3): <fmt:formatNumber type="number" 
        maxFractionDigits="3" value="${balance}" /></p>
<p>Formatted Number (4): <fmt:formatNumber type="number" 
        groupingUsed="false" value="${balance}" /></p>
<p>Formatted Number (5): <fmt:formatNumber type="percent" 
        maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (6): <fmt:formatNumber type="percent" 
        minFractionDigits="10" value="${balance}" /></p>
<p>Formatted Number (7): <fmt:formatNumber type="percent" 
        maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (8): <fmt:formatNumber type="number" 
        pattern="###.###E0" value="${balance}" /></p>

Result

Formatted Number (1): £120,000.23

Formatted Number (2): 000.231

Formatted Number (3): 120,000.231

Formatted Number (4): 120000.231

Formatted Number (5): 023%

Formatted Number (6): 12,000,023.0900000000%

Formatted Number (7): 023%

Formatted Number (8): 120E3

Solution 11 - Css

Another solution with pure CSS+HTML and the pseudo-class :lang().

Use some HTML to mark up the number with the classes thousands-separator and decimal-separator:

<html lang="es">
  Spanish: 1<span class="thousands-separator">200</span><span class="thousands-separator">000</span><span class="decimal-separator">.</span>50
</html>

Use the lang pseudo-class to format the number.

/* Spanish */
.thousands-separator:lang(es):before{
  content: ".";
}
.decimal-separator:lang(es){
  visibility: hidden;
  position: relative;
}
.decimal-separator:lang(es):before{
  position: absolute;
  visibility: visible;
  content: ",";
}

/* English and Mexican Spanish */
.thousands-separator:lang(en):before, .thousands-separator:lang(es-MX):before{
  content: ",";
}

Codepen: https://codepen.io/danielblazquez/pen/qBqVjGy

Solution 12 - Css

…A few years later…

I use this to format a number with a small space as thousands separator:

theNumber.toString().replaceAll(/(\d)(?=(?:\d\d\d)+$)/g, '$1\u202f')

It will insert a small space after each digit which is followed by a number of digits, where the digit count is a multiple of 3.

  • (\d) => A digit. In brackets to remember the digit

  • (?=) => Positiv look-ahead

  • (?:\d\d\d) => Three digits. Just grouped.

  • + => At least one group of three digits have to be present

  • $ => End of string/number

  • $1 => replaced by the remembered digit

  • \u202f => Unicode notation for a small blank

As pointed out: That's Javascript, not CSS.

Solution 13 - Css

Another js solution to improve the work of Skeeve:

<input type="text" onkeyup="this.value=this.value.toString().replaceAll(/[^\d]/g, '').replaceAll(/(\d)(?=(?:\d\d\d)+$)/g, '$1\u202f')" pattern="[0-9\s]*">

Solution 14 - Css

The closest thing I could find is the <input type="number" /> tag, which does do formatting in plain HTML but is also an input field. To make it look like plain text, you could use a bit of CSS.

Unfortunately I don't know how to fix the right margin without JavaScript or using a monospace font and set the width attribute server side.

HTML:

<p>In <input type="number" value="1.223" readonly="readonly" size="1" /> line</p>

CSS:

p {font-family: verdana;}
input {
  font-family: verdana;
  font-size: 16px;
}
input[readonly] {
  border: 0;
  padding: 0;
  margin: 0;
  min-width: 3em;
  font-size: 16px; 
}
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDonView Question on Stackoverflow
Solution 1 - CssYoannView Answer on Stackoverflow
Solution 2 - CssCascadiaJSView Answer on Stackoverflow
Solution 3 - CssAnton Y. ReuchenkoView Answer on Stackoverflow
Solution 4 - CssRossView Answer on Stackoverflow
Solution 5 - CssmreqView Answer on Stackoverflow
Solution 6 - CssitpastornView Answer on Stackoverflow
Solution 7 - CssDanielBlazquezView Answer on Stackoverflow
Solution 8 - CssFlorin FrăticăView Answer on Stackoverflow
Solution 9 - CssQorbaniView Answer on Stackoverflow
Solution 10 - CssMohammad JavedView Answer on Stackoverflow
Solution 11 - CssDanielBlazquezView Answer on Stackoverflow
Solution 12 - CssSkeeveView Answer on Stackoverflow
Solution 13 - CssvacsatiView Answer on Stackoverflow
Solution 14 - CssCode4R7View Answer on Stackoverflow