Reading Time: 7 minutes

In today's Training Talk, we're going to show you how to convert and format numbers as strings or numbers in . Something we've noticed confuses people sometimes is the use of the hash symbol (#) versus the zero symbol (0) in the conversion format.

In this demo, Ethan will show some examples based on the java.text.DecimalFormat class format, which you can look at the documentation to learn more about. The first example shows a decimal number that goes out to the four decimal place.

latest report
Learn why we are the Leaders in management and

You will also learn:

  • How to handle decimal numbers that are smaller than 1, to decide if a leading 0 should be included in the number formatting (for example, whether to print out 0.80, 0.8, .8, or .80). This type of formatting is useful if you need to format decimals as currency.
  • How to format the Integer part of a number, such as if and where to place commas “,” or other symbols to format your numbers for a particular locale.

To help you with the demo you can follow Ethan by reading below:

First of all, here's DataWeave. We have a decimal number that goes out to the fourth decimal place (1234567.8019). If we just format the number directly into a string, it gets directly converted into a string (“1234567.8019”), and so that decimal could be converted into a string and then operated on. But if you apply a different format (“#,###.###”), in this case— first of all, the comma specifies where the thousandth position should be, and typically that will be in the thousandths spot (1,234,567). You can see the thousands place changing  (“1,23,45,67.8019”) as I change the comma back and forth. Because I'm using the format with three hash tags, that will match x out to three decimal places.

The hashtag (#) means to print any non-zero digits, especially in the final place. In this case, it's the thousandths place (.###). If I use two hashtags (.##), then in the hundredths place, the zero is matched, but it is not printed out (.8). If I put a zero in the tenths place (.0#), again, the hashtag in the hundredths place is matching the zero, and the zero is not printed out (.8).

If I change the hundredths place hash to a zero (.00), then the zero is printed out (.80). And if I go one more decimal place to the thousandths place (.000), the thousandths place prints out 2  because the 19  in .8019  is rounded up to 2 (.802).

Let's take a look at decimals that don't have any integer part to them. If I look at 0.8019 and apply different formatting, then if I apply all hashes (#.##) to two decimal places, again, the hundredths place zero does not print out (0.8). If I try to put a zero in the hundredths place (#.#0), that is not an allowable format. If I put a zero and a hash (#.0#), again, the hundredths place is a zero, so it doesn't print out (.8). If I change the hundredths place hash to a zero (#.00), then the zero does print out (0.80). I can take this out additional decimal places, and eventually, I match the entire number (#.0000####), and any later hashtags don't print out as zeros. If I replace the hashtags with zeros instead (#.00000000), then additional zeroes are padded out to the number of significant figures that are listed (0.8019000).

Finally, if you want to deal with internationalization, you could take the number that's formatted in the Java standard, and then I could just do a replacement to replace the commas with periods and the period with a comma like you would see in some European countries like Spain.

Have questions for the Training team? Drop it in our forum or Training Twitter using the hashtag #TrainingTalks and stay tuned!