How can we nicely format a floating number into String by removing unnecessary zeros?

0 votes
If I have numbers like 24.0000000, 0.2900 and 0.000, then I want output as 24, 0.29 and 0. How can we achieve this?
Jun 6, 2018 in Java by Daisy
• 8,040 points
29 views

1 answer to this question.

0 votes

public static String fmt(double d) {

if(d == (long) d)

return String.format("%d",(long)d);

else

return String.format("%s",d);

 }
Call this method and pass the number to the method:
String ans=fmt(24.00000);
answered Jun 6, 2018 by parth295
• 4,600 points

Related Questions In Java

0 votes
1 answer

How can we add leading zeros to the number in Java?

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
183 views
0 votes
1 answer

How can we remove html tags from a string?

You can use HTML parser. This is ...READ MORE

answered Jun 4, 2018 in Java by sophia
• 1,400 points
121 views
0 votes
1 answer

How can we split a comma separated String in Java?

String str = "..."; // write the ...READ MORE

answered Jun 12, 2018 in Java by parth295
• 4,600 points
51 views
0 votes
1 answer

How can we encrypt a String in Java?

Let's assume the bytes to encrypt are ...READ MORE

answered Oct 26, 2018 in Java by Sushmita
• 6,850 points
15 views
0 votes
1 answer

Result of character addition in Java

The result of adding Java chars, shorts, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,540 points
44 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered 1 day ago in Java by Sirajul
• 18,450 points
111 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,540 points
42 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
64 views
0 votes
3 answers

How can we use java.String.format in Scala?

String aString = "world"; int aInt = 20; String.format("Hello, ...READ MORE

answered Aug 29, 2018 in Java by Daisy
• 8,040 points
199 views
0 votes
2 answers

How can we Split Java String by New Line?

If you don’t want empty lines: String.split("[\\r\\n]+") READ MORE

answered Aug 22, 2018 in Java by Daisy
• 8,040 points
326 views