• Post Reply Bookmark Topic Watch Topic
  • New Topic

DecimalFormat and leading zeros.  RSS feed

 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I have this problem.

I have numbers with 1, 2 or 3 digits (always integers).
I want to format those numbers with 2 leading zeros.
For example, if the number is 2 (1 digit), I want to obtain 001.
If the number is 39 (2 digits) I want to obtain 0039.

Is there an easy way to obtain this using the DecimalFormat class?

Thank you in advance.

P.S.: Actually I'm using the pattern "00000" but it doesn't always work, because if the number has digits < 3, the leading zeros are more than 2.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably easiest done by using the class java.util.Formatter ( see the Javadoc for details) or using the String.format() method which uses java.util.Formatter behind the scenes.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just prefix the number with two zeroes ?

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato

If the requirement is to always have 2 leading zeros, regardless of the actual number, Joanne's solution is the simplest you are going to find!
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I need to use the pattern syntax from the DecimalFormat class.
Basically this number would be formatted by a <f:convertNumber /> tag in a JSF page.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Perini wrote:No, I need to use the pattern syntax from the DecimalFormat class.
Basically this number would be formatted by a <f:convertNumber /> tag in a JSF page.

So put two zeroes at the start of your pattern.
You just need to read the DecimalFormat javadoc to find out how to make sure they are treated as normal zeroes and not formatting characters.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Perini wrote:No, I need to use the pattern syntax from the DecimalFormat class.
Basically this number would be formatted by a <f:convertNumber /> tag in a JSF page.


I should have guessed you were using JSF ! Thanks for sharing the context.
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Why not just prefix the number with two zeroes ?



Because the number is passed to a view by a backing bean and manually concatenating 2 zeros has no effect during the print of the value. The number is automatically reconverted from a String to a Long: the converter removes the 2 leading zeros automatically.
Sorry for not having specified the operating domain in the OP.
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Renato Perini wrote:No, I need to use the pattern syntax from the DecimalFormat class.
Basically this number would be formatted by a <f:convertNumber /> tag in a JSF page.

So put two zeroes at the start of your pattern.
You just need to read the DecimalFormat javadoc to find out how to make sure they are treated as normal zeroes and not formatting characters.


It doesn't work either. If I put two zeros in the pattern, when the number has 3 digits, the converter would print the number with 1 leading zero only.
For example, assuming the pattern is: "00" and the number is 348 (three digits) the result would be simply 349, contrary to the result I would like to obtain: 00349

I have read the documentation for the DecimalFormat class but unfortunately I can't figure out the correct pattern to use.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Perini wrote:I have read the documentation for the DecimalFormat class but unfortunately I can't figure out the correct pattern to use.

Did you read this
' Prefix or suffix No Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Renato Perini wrote:I have read the documentation for the DecimalFormat class but unfortunately I can't figure out the correct pattern to use.

Did you read this
' Prefix or suffix No Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".


Yuuhuuu. It works!

I have written this simple program to test it:


and works like expected. Thank you very much Joanne!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!