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

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

Related

How to make a 4 digit integer [duplicate]

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()

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));

Add binary numbers like decimal numbers in Java. eg 0101 + 0110 = 0211 [duplicate]

This question already has answers here:
Adding binary numbers
(22 answers)
Closed 7 years ago.
I needed to write a program which adds binary numbers as if they were decimal. But it isn't working like I expected.
int i = 0101, j=0001;
System.out.println(i+j);
I expected the answer to be either 6 (i.e decimal of sum of 0101 and 0001) or maybe 0102 (as I am adding them as simple decimal numbers). But unexpectedly, I am getting 66. Can anybody kindly explain this? Or may be help me with the code to add two binary numbers as decimal numbers.
You're using octal literals, i.e. 0101 = 65 = 1 * 8² + 1. To use binary literals use the following notation:
int i = 0b101, j = 0b1;
If you want to print a int as binary, use Integer.toBinaryString to get the string representation of a int in binary.
int num1 = Integer.parseInt(Integer.toString(i),2);
int num2 = Integer.parseInt(Integer.toString(j),2);
System.out.println(num1+num2);
First we need to convert the number from binary to decimal. And for that you need to parse it using the parse commands for the Integer wrapper class. Then you can add.
The format Integer.parseInt(String,radix) can be used to convert any string of digits to the base radix.

Format decimal values with String in Java

I am new to android and java programming, so please forgive me if this is an easy question. My problem is to add % in end of the decimal value. I will get double value and don't know how much digits after decimal point, i need to convert to two digits after decimal point and finally need to add % at last position. The problem is, I will format the double value only once. I have tried like this DecimalFormat ("#.##%"), but decimal points moved two digits forward. Please help me to get out of this problem.
Using DecimalFormat ("#.##%"),
Actual : 34.142545
Output : 3414.25%
If you use % in format, it means that you want to convert value to percentage, but you need to remember that value 1 in percentage world is equal to 100% so value you use will be automatically multiplied by 100. To avoid this behaviour you can change % into literal by quoting it with apostrophes '%'
new DecimalFormat("#.##'%'");
By adding the % in the format, you multiply by 100 and add the % character. This is why it looks like the decimal point moves.
You can use something like:
String output = new DecimalFormat("#.##").format(input) + "%";
An alternative is to divide by 100 before formatting:
String output = new DecimalFormat("#.##%").format(input / 100.0);

BigDecimal string formatting with thousands separator (###.###,##) [duplicate]

This question already has answers here:
How to print formatted BigDecimal values?
(7 answers)
Closed 9 years ago.
I have a BigDecimal whose value I'd like to convert to a string and NOT lose any precision.
The format I'd like to use is ###.###,## (comma for thousands, period for decimals).
The only way I've made this work is using
DecimalFormat formatter = new DecimalFormat("###.###,##");
formatter.format(bd.doubleValue());
... but I'm afraid I might lose information this way, and precision is a must because I'm dealing with currency (every penny counts).
Additional information: I will only be dealing with sums of up to 1 million if that is of any help.
To be on the safe side, you could multiple your BigDecimal by 100.
Then get its intValue -> say N. Then get N/100 and N%100.
This way you cannot lose precision (N <= 100 million cannot overflow int).

Categories