[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Viable NumberFormat solution in JAVA?

Posted on 2005-04-24
7
Medium Priority
?
244 Views
Last Modified: 2010-03-31
Hello there,

I'm very new to Java so bear with me please.  I wrote a little script to find out ones weight on the moon and it works to a certain degree.  I'd rather get rid of the trailing notation at the end though.  Say I insert 61lbs, it outputs 10.126000000000001.  How could I about just displaying the set of numers, not the stuff after the decimal point?  I do know in php i'd use something like printf to get the job done.  I guess I must also say, that it should roundup somehow, if that is possible :)

Thanks in advance!
0
Comment
Question by:lamerhooDJV
7 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 13854147
You mean the ceiling, say 10 in your case, of the output? If yes, Math.ceil() should work for you.
Or do you mean to output 10.126?


0
 
LVL 8

Expert Comment

by:koppcha
ID: 13854206
This will do in either case

for 10
import java.text.*;
public class Trim {

      public static void main(String[] args) {
            DecimalFormat df1 = new DecimalFormat ("#0");
            double f1 = 10.126000000000001;
            System.out.println(df1.format(f1));
      }
}

for 10.13(Rounding is done)
import java.text.*;
public class Trim {

      public static void main(String[] args) {
            DecimalFormat df1 = new DecimalFormat ("#0.00");
            double f1 = 10.126000000000001;
            System.out.println(df1.format(f1));
      }
}

You just have to change the DecimalFormat to what ever the pattern you want
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13854214
Well, it roundsup no matter what (EG: 10.126000000000) would still be 10, right?) and so, how would I go about that :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13854215
kopcha, I'll take a look :)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 13854606
>>I do know in php i'd use something like printf

You can in Java 1.5

System.out.printf("%.2f\n", Math.PI);

else you can do

System.out.println(new DecimalFormat("0.00").format(Math.PI));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13854897
This is another way of doing it:


double d = Math.PI;
BigDecimal pi = BigDecimal.valueOf(d);
pi = pi.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.print(pi);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13881102
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question