Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format an Integer to a four-zero-left string?

I'm trying to do something like it in java (Eclipse Indigo):

input - 16 (integer);
Output - "0016" ;

input - 201 (integer);
Output - "0201" ;

intput - 1716 (integer);
Output - "1716" ;

In VB.net I could use:

dim num as integer
dim str as string

str = Format(num, "0000")

How can I do the same in java? Thanks in advance...

like image 405
Alex Avatar asked Feb 01 '12 19:02

Alex


People also ask

How do you write in string format?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.


1 Answers

final String s = String.format("%04d", yourNumber).

Note that there is a locale-sensitive version of this method as well.

like image 127
mre Avatar answered Oct 22 '22 21:10

mre