Limiting double's decimals

This is probably a simple one, but here goes.

Normally java will display an irrational number in the following form "37.666666666666664"

How do I edit this so that it is "37.66" or "37.67"? Since I am writing my output to a text file, this is quite bothersome that there are so many decimals since it looks rather ugly

[337 byte] By [Steven1350a] at [2007-11-27 8:47:24]
# 1
http://java.sun.com/docs/books/tutorial/i18n/format/numberintro.html~
yawmarka at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
hunter9000a at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 3
@Steven1350: Seeing how you previous thread ended, and this one is likely to end just like that, here's something to read: http://www.catb.org/~esr/faqs/smart-questions.html#courtesy (and also the paragraph beneath it)
prometheuzza at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 4

Those classes seem rather complex for something so simple. They want to take a String (not a double) as an input. If this is the case, I can easily make a method which can accomplish what I want. I was hoping for a static method which has a double in the parameter, and it would return the modded double.

Thanks anyways.

@prometheuzz <-- Why are you wasting my time with that?

Steven1350a at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> Those classes seem rather complex for something so

> simple. They want to take a String (not a double) as

> an input. If this is the case, I can easily make a

> method which can accomplish what I want. I was hoping

> for a static method which has a double in the

> parameter, and it would return the modded double.

>

> Thanks anyways.

You need to provide that String once: it's the pattern of how your primitive double should look like. After that you just callString s = yourDecimalFormatInstance.format(aDouble);

and it's properly formatted.

> @prometheuzz <-- Why are you wasting my time with that?

By pulling a response out of you, which you neglected in your first post.

And I have read the document (some parts more than once) and I don't find that it wastes any body's time.

prometheuzza at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 6

> Those classes seem rather complex for something so

> simple. They want to take a String (not a double) as

> an input. If this is the case, I can easily make a

> method which can accomplish what I want. I was hoping

> for a static method which has a double in the

> parameter, and it would return the modded double.

>

> Thanks anyways.

>

> @prometheuzz <-- Why are you wasting my time with

> that?

I don't see what's so complex about it, you just give it a pattern, and it formats the output based on it. There's even a tutorial that shows you how to construct the patterns: http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

Here's a sample:

String pattern = "#.##";

double value = 1234.56789;

DecimalFormat myFormatter = new DecimalFormat(pattern);

String output = myFormatter.format(value);

System.out.println(value + " " + pattern + " " + output);

That prints all the digits before the decimal, and only two after the decimal, rounding the rest of them. This is handy because you can keep that same object around and format many different numbers with it. You can create many different formats for different purposes and use whichever one you need.

Prometeuzz wasn't trying to waste your time, he was trying to help you not waste yours. The people around here volunteer their time to answer your questions because they like to help people. If you don't show some gratitude for the help you get, then everyone who reads that thread will be a lot less willing to help in the future. Why take the time to help someone who won't appreciate it? You can ignore his advice if you want, but you'll only be hurting yourself.

hunter9000a at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 7

bah! I already took the time and made a nice class to do all the work

public class DoubleParser {

/**

* Takes a double and reduces the number of decimal places it has

* @param number The input double

* @param numReduce The number of decimals to have in the output

* @return The redcued decimal double number

*/

public static double reduce(double number, int numReduce){

Double doub = new Double(number);

String str = Double.toString(number);

int index=-1;

//Search the String for a decimal (".") and record its index

for(int i=0; i<str.length();i++){

if(str.charAt(i)=='.'){

index=i+1;

}

}

//Make sure that numReduce is safe

boolean isSafe=false;

while(!isSafe){

if(index+numReduce > doub.toString().length()){

numReduce--;

}

else{

isSafe=true;

}

}

//Catch potiental errors

if(numReduce < 0){

try {

throw new Exception("Cannot reduce to less the 0 decimals");

} catch (Exception e) {

e.printStackTrace();

System.exit(1);

}

}

else{

if(index!=-1 && numReduce>=0){

str = str.substring(0,index+numReduce);

}

//Begining of the string

else if(index==0){

str = "0"+str+"0";

str = str.substring(0,index+numReduce);

}

//End of the string

else if (index==str.length()-1){

str=str+"0";

str = str.substring(0,index+numReduce);

}

return Double.parseDouble(str);

}

//Unreachable

return 0;

}

}

It gets the same job done (only except it a bit longer then your snippet

Message was edited by:

Steven1350

Steven1350a at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 8

> bah! I already took the time and made a nice class to

> do all the work

>

> ...

I suggest you use Java's built-in classes for this as previously demonstrated.

The following line:System.out.println(DoubleParser.reduce(0.12999, 2));

produces 0.12 as output: I don't think that's desirable!

prometheuzza at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 9
I realize that, however is good for what I am gonna use it for
Steven1350a at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...
# 10
> I realize that, however is good for what I am gonna use it forSuit yourself.
prometheuzza at 2007-7-12 20:52:12 > top of Java-index,Java Essentials,Java Programming...