Solved

Need Help Rounding

Posted on 2011-09-30
9
206 Views
Last Modified: 2012-05-12
I need help with a rounding problem. I want to take a number and convert it with two decimal places.

For instance, 345.667 would end up being 345.67; and 345 would end up being 345.00.

I have the following:

double number = Marth.round(number * 100.0)/100.0;

This works for most numbers, but when I have a whole number it converts it 345.0 with only one decimal place.

Any suggestions?
0
Comment
Question by:InquisitiveProgrammer
  • 7
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36895498

something like that:



DecimalFormat f = new DecimalFormat("#####.##");

double d = 345.6789;
String s = f.format(d);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895507

This does what you want:

        DecimalFormat fff = new DecimalFormat("#####.00");
        System.out.println(fff.format(345.6789));
        System.out.println(fff.format(365.0));

Open in new window


345.68
365.00

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895510
More examples:

 DecimalFormat fff = new DecimalFormat("#####.00");
        System.out.println(fff.format(345.6789));

               System.out.println(fff.format(365.0));

        System.out.println(fff.format(365));

Open in new window



Output:

345.68
365.00
365.00

Open in new window

0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36895512
Is there no way to keep the number as a double? Without converting to string?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 36895522
What you are asking in fact applies to strings - for double number there is no difference between

double d = 365;

double d = 365.0;

double d = 365.00;

those are all the same double - it is hen you want to print them where the difference come.

You of course can say


double d = Double.parseDouble(fff.format(345));

but it does not make much sense - if you could explain in  what context you mmay need it

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895529
your rounding will work in the sense of giving number with no more that two digits

double number = Marth.round(number * 100.0)/100.0;

but when you apply it to number with less decimal digits - I don't see where is the program unless you want to print it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895530
but when you apply it to number with less decimal digits - I don't see where is the problem unless you want to print it
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36895538
I'm not getting the decimals to print out correctly.

Here is my function:

 
public String roundTwoDecimals(double number) {
    	number = Math.round(number * 100.0)/100.0;
    	DecimalFormat twoDecForm = new DecimalFormat("###############.##");
    	String dollarAmount = twoDecForm.format(number);
    	//return Double.valueOf(twoDecForm.format(number));
    	return dollarAmount;
    }

Open in new window


This will work if I have a number like 34.5677, but it still won't work for when I have a number like 64 or 54.6
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36895542
you should have format "#####.00" - see my tested example
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 124
firstswap challenge 20 67
servlet  URL Rewriting 1 26
MySqlDump not dumping triggers 1 17
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now