How to make a 4 digit integer [duplicate] - java

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 5 months ago.
I want to make an integer value from the range 1-9999.
But the integer values should be always in 4 digits.
Like 0001, 0002, 0015, 0156, 1578

You can probably use String format of the integer you willing to, like this:
String.format("%04d", your integer));
This will always show missing 0 on the left on digits like 01 or 1.

System.out.format("%04d%n", n);

If you want to print 4 digit integer:
System.out.printf("%04d%n",your integer);
If you want to store in a variable:
String str=String.format("%04d", your integer));
Read more details about String format()

Related

is there a way to get always 2-digits values with TimeStamp in Java? [duplicate]

This question already has answers here:
How to write integer with two digits? [duplicate]
(3 answers)
How to format a number 0..9 to display with 2 digits (it's NOT a date)
(7 answers)
Closed 11 months ago.
Let's say i have several TimeStamp objects and i want to know the difference between 2 of them in terms of minutes and seconds.
TimeStamp timestamp1 = new TimeStamp(System.currentTimeMillis());
TimeStamp timestamp2 = new TimeStamp(System.currentTimeMillis());
if i use the method getSeconds (but the concept can be adapted to minutes and hours too), for example, the expected output is "SS".
But when i try to get the difference between the 2 objects (something like:
long differenceInSeconds = (timestamp1.getseconds() - timestamp2.getSeconds());
Now the expected output is just the difference between 2 numbers, so clearly it can be 0, 5, 13, ...
What if i wanted to always have values formatted in XX?
0 would be 00, 13 would be 13 and going on.
Maybe there is some class, or method,regarding timestamp which i'am unaware of?
I think the reason why it is not padded is because the Method returns an int value, which is not padded by default. The only way to get a padded value is converting your number to a string and then add leading zeros.
What you want can be achieved in the following way
long differenceInSeconds = (timestamp1.getseconds() - timestamp2.getSeconds());
String.format("%02d", differenceInSeconds)
The flag %02d means it will convert it into a 2 digit string if the passed number is less than 2 digits. So if you pass 5 it will be converted to string 05. If you pass 13 it will remain 13.

Accessing digits of a binary number [duplicate]

This question already has answers here:
Converting an int to a binary string representation in Java?
(19 answers)
Closed 3 years ago.
Let's say i have a binary number initialized like this:
int y=0b110101;
How could i convert 110101 to a String?
I would like this:
String str1 = Integer.toString(y);
System.out.println(str1);
to give result 110101 or 0b110101 and not 53.
Integer.toBinaryString(y) would give you 110101 in your case and you can prepend the 0b to the result if you'd like that.

Expected Double value to display two zeros after precision value [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have a scenario where we need to display two zeros i.e 00 after the precision for double value. But unfortunately Java is not returning the second zero.
I have tried the below way but it is returning only one zero after decimal.
double inputvalue = 12345.00;
double d = ((BigDecimal.valueOf(inputvalue)).setScale(2,RoundingMode.UP)).doubleValue();
Expected output: 12345.00 but it is returning 1234.0.
Have you considered using a string to display the value. e.g.
String.format( "%.2f", d)

Round double value to 2 decimal digits [duplicate]

This question already has answers here:
Format double value in scientific notation
(4 answers)
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have been working with big numbers (with more that 20 digits) and for that reason I use Double . The form I get the numbers is like this 8.653762536765E28.
What I want to do is just display the first 2 decimal digits. I want it like 8.65E28.
I tried to find about formatting double values but I wan't able to do it. The result I was getting was 86537...12312.00 .
What do you thing is a good approach for this case? How can I manage only the digits in front of the E (the base) and not the whole number?
I think this is what you want to achieve:
double d = 8.653762536765E28;
DecimalFormat df = new DecimalFormat("0.00E0");
System.out.print(df.format(d));

Display numbers with leading zeros (0) in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Add leading zeroes to number in Java?
Say that I have to add two integers one being 0001 and the other 0002. If I add it in java then I get 3 however I would like 0003. Would I have to make a loop to map out the zeros or is there an easier way.
Don't confuse numbers with String representation of numbers. Your question revolves around the latter -- how to represent a number as a String with leading zeros, and there are several possible solutions including using a DecimalFormat object or String.format(...).
i.e.,
int myInt = 5;
String myStringRepOfInt = String.format("%05d", myInt);
System.out.println("Using String.format: " + myStringRepOfInt);
DecimalFormat decimalFormat = new DecimalFormat("00000");
System.out.println("Using DecimalFormat: " + decimalFormat.format(myInt));
you can add a left pad with zeros after having the result.
String.format("%05d", result);
for zero-padding with length=5.
EDIT: i to removed the previous EDIT, it was totally wrong :#
This will help you
String.format (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax)
In your case it will be: String.format("%03d", num) - 0 - to pad with zeros, 3 - to set width to 3

Categories