left-icon

Localization for .NET Succinctly®
by Jonas Gauffin

Previous
Chapter

of
A
A
A

CHAPTER 5

Numbers

Numbers


As first glance, numbers might seem to be the same for different cultures. But they do differ when it comes to separators and grouping.

One million comma fifty is in Swedish “1 000 000,50” while it’s “1,000,000.50” in the United States.

.NET

Among the others, in .NET we use the ToString() method to do the formatting. I recommend that you try to use the built-in formatters instead of manually defining the grouping etc.

Let’s print a million and a half:

var se = new CultureInfo("sv-SE");

var us = new CultureInfo("en-US");

var number = 1000000.5;

Console.WriteLine("SE: {0}", number.ToString("N", se));

Console.WriteLine("US: {0}", number.ToString("N", us));

The result follows:

  1. Number output

You could have done the same example by specifying the grouping manually:

var se = new CultureInfo("sv-se");

var us = new CultureInfo("en-us");

var number = 1000000.5;

Console.WriteLine("SE: {0}", number.ToString(@"#,#.0", se));

Console.WriteLine("US: {0}", number.ToString(@"#,#.0", us));

This results in:

Numbers formatting

  1. Numbers formatting

The comma tells .NET to use digit grouping, and the “.0” is used to tell how many decimals to use. You can read more about number formatting in MSDN.

JavaScript

At first, formatting numbers in JavaScript seems to be easy. There is a function called toLocaleString(culture), which should work just fine. Unfortunately, the support is not great in all browsers. And it also depends on which languages the user has activated in the web browser.

Let’s take a simple example. Add the following code into an empty HTML page.

<script type="text/javascript">

    var number = 1000000.50;

    document.writeln(number.toLocaleString('sv-SE') + "<br>");

    document.writeln(number.toLocaleString('en-US') + "<br>");

</script>

The output from that script looks quite nice in Google Chrome, as shown in Figure 34:

Chrome output

  1. Chrome output

But the output in IE is a bit lacking (but IE get a bonus point for the decimal formatting), as shown in Figure 35:

Internet Explorer output

  1. Internet Explorer output

The Swedish formatting is used for the U.S. culture, too. The reason is that IE ignores the culture argument and uses the culture that is configured in Windows.

Globalize

Since we have Globalize, we can get a bit more straightforward conversion.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script src="Scripts/jquery-1.9.1.min.js"></script>

    <script src="Scripts/jquery.globalize/globalize.js"></script>

    <script src="Scripts/jquery.globalize/cultures/globalize.culture.se-SE.js"></script>

</head>

<body>

    <script type="text/javascript">

        Globalize.culture("se-SE");

        document.writeln(Globalize.format(1000000.1, "n") + "<br>");

        Globalize.culture("en-US");

        document.write(Globalize.format(1000000.1, "n"));

    </script>

</body>

</html>

The result follows:

Globalize output

  1. Globalize output
Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.