• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DecimalFormat and leading zeros.

 
Greenhorn
Posts: 18
Eclipse IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just prefix the number with two zeroes ?

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic