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,050 points
35 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,640 points

Related Questions In Java

0 votes
2 answers

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

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26 in Java by Sirajul
• 29,940 points
306 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
128 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,640 points
59 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,880 points
18 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22 in Java by Sirajul
• 29,940 points
54 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 Aug 20 in Java by Sirajul
• 29,940 points
164 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
47 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
75 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,050 points
225 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,050 points
474 views