Java String.format() leading zeros and 1 decimal place [duplicate] - java

This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0

Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent

You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();

Related

Printing out double with variable length after decimal point [duplicate]

This question already has answers here:
Use DecimalFormat to get varying amount of decimal places
(4 answers)
Closed 1 year ago.
I was searching for a solution of how to print double with variable length. Means: user will define how many digits he wants after the decimal point, but without success.
I've come to something like, but it doesn't work :
num - double
dec(length) - integer
System.out.printf("%.(%d)f\n", num, dec);
Are you looking for something like the following?
Maybe solution 2 is suitable for you.
Solution 1:
System.out.printf("%.2f", val); // "%.2f" it's a string so you can make it in several ways...eg: "%."+ dec + "f";
Solution 2:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2); // you can use int variable instead of 2.. eg: df.setMaximumFractionDigits(dec);
System.out.println(df.format(decimalNumber));

Java String format float with and without 0 [duplicate]

This question already has answers here:
Is it possible to use String.format for a conditional decimal point?
(2 answers)
How to nicely format floating numbers to string without unnecessary decimal 0's
(29 answers)
Closed 3 years ago.
I have a float number, let's say:
Float number = 2.667f;
String.format("%.1f", number)
will produce:
2,7
but in case of
Float number = 2.0f;
it will produce:
2,0
is it possible to instruct String.format to avoid 0 after comma in case of integer numbers in order to receive for example 2 in the mentioned case.
You can use DecimalFormat with # symbol:
DecimalFormat df = new DecimalFormat("#.#");
System.out.println(df.format(2.1f)); // 2.1
System.out.println(df.format(2.0f)); // 2

How to take 2 digits number in string and convert it to int [duplicate]

This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.

How to get exact decimal value from bigdecimal in java [duplicate]

This question already has answers here:
"new BigDecimal(13.3D)" results in imprecise "13.3000000000000007105.."?
(5 answers)
Closed 6 years ago.
I am trying to get number of digits after decimal point in BigDecimal value.
BigDecimal big = new BigDecimal(1231235612.45);
String[] str = big.toPlainString().split("\\.");
System.out.println(" Decimal Value: " + str[1]);
Using this I am getting following output -
Decimal Value: 4500000476837158203125.
Actualy I want to display only 45 as per the original BigDecimal value (1231235612.45).
So, my expected output is Decimal Value: 45.
But, while conversion it adds more digits after decimal points.
Is there any method or code to get exact same value from BigDecimal?
Don't use the double Constructor of BigDecimal (See Javadoc, it is discouraged).
use String constructor
new BigDecimal("1231235612.45");
or use MathContext
new BigDecimal(1231235612.45, MathContext.DECIMAL64);

How to add Leading Zero with getMonth in Java (Android) [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Adding a leading zero to a large string in Java
(2 answers)
Closed 8 years ago.
I'm using an int variable:
month = dp.getMonth() + 1;
currently getting an output of "2" and when I do the following:
if (month<10){
month = '0'+month;
};
I get: 50.
Your problem is that your '0' char is being coerced to an integer. Since '0' has an ASCII value of 48, you're getting 48 + 2 = 50.
Note that what you're trying to do won't work - you can't add a leading 0 to month, as month is a number. A leading zero only makes sense in a string representation of a number.
As explained in this answer, here's how to produce a zero-padded number:
String.format("%02d", month);

Categories