Removing unneeded zeroes [duplicate] - java

This question already has answers here:
Remove trailing zero in Java
(12 answers)
Closed 7 years ago.
How to delete zeroes in java (android) only if there is no non-zero value following them?
The value is stored in double and then later in string, so working on these variables would be best.
Example:
I have 12.50000
I want to have 12.5
Example2:
I have 65.4030
I want to have 65.403

Try this :
String s = 12.50000;
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");

Related

BigDecimal remove zeros only after decimal [duplicate]

This question already has answers here:
Removing trailing zeros from BigDecimal in Java
(6 answers)
Closed 3 years ago.
I am sending Bigdecimal numbers in json object. I have an issue with zeros.
I tried stripTrailingzeros() functions but it is not working as expected
30.000 -> 3E+1 expectaion is 30
Any suggestions?
Suppose the number in BigDecimal val = 3E+1 (That is val = new BigDecimal("30.000").stripTrailingZeros();).
When you print this then out would be 3E+1.
In order to make it plain string toPlainString will help you.
System.out.println(val.toPlainString()); // 30

String format query [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 5 years ago.
What does it represents in java?
String.format("%01000d",0)
It prints 0 thousands times. But can anyone help how does it actually works. What does "%01000d" represents?
The first argument is a format string, here you can find the sintax:
https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Extract/Trim part of string, Java [duplicate]

This question already has answers here:
Java - removing first character of a string
(14 answers)
Closed 5 years ago.
Say String x = MynameisMary;
I want String Y to be "Mary"
How do i remove the first 8 characters to make Y equal to Mary??
You could use the substring method:
String result = x.substring(8);

Why is Long value not printed correctly in java? [duplicate]

This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 6 years ago.
i have declared Long checkRandom=0101010101l;
but when I am printing checkRandom it printed checkRandom==17043521
what was the reason its taking checkRandom=17043521 not checkRandom=0101010101
Just remove the leading 0:
Long checkRandom=101010101l

Simple check operation is not working in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality

Categories