Number Formatting of a doiuble to 2 decimal position

S

stu

I tried with setMinimumFractionDigits(int) (from
java.text.NumberFormat) to get 22.22 of 22.222221.

I tried it on the double variable as well as after the double has been
converted to string. What am I doing wrong? Here is my code:

Note that FahrenheitDegree was declared as int.

Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
Double cs = celsiusDegree.setMinimumFractionDigits(2);
String celsiusString = new Double(cs).toString();

//String cstr = celsiusString.setMinimumFractionDigits(2);

String message = String.format(FahrenheitString + " degree Fahrenheit =
%s degree Celsius.", celsiusString);

//String message = String.format(FahrenheitString + " degree Fahrenheit
= %s degree Celsius.", cstr);
 
M

Mark Thornton

stu said:
I tried with setMinimumFractionDigits(int) (from
java.text.NumberFormat) to get 22.22 of 22.222221.

I tried it on the double variable as well as after the double has been
converted to string. What am I doing wrong? Here is my code:

Note that FahrenheitDegree was declared as int.

Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
Double cs = celsiusDegree.setMinimumFractionDigits(2);
String celsiusString = new Double(cs).toString();

//String cstr = celsiusString.setMinimumFractionDigits(2);

String message = String.format(FahrenheitString + " degree Fahrenheit =
%s degree Celsius.", celsiusString);

//String message = String.format(FahrenheitString + " degree Fahrenheit
= %s degree Celsius.", cstr);

setMaximumFractionDigits!
 
S

stu

I should use setMaximumFractionDigits(int) but the same error anyway:
double cannot be dereferenced.
 
T

Thomas Schodt

stu said:
I tried with setMinimumFractionDigits(int) (from
java.text.NumberFormat) to get 22.22 of 22.222221.

Maybe you want setMaximumFractionDigits() ?
 
S

stu

setMaximumFractionDigits!

I realized that but both of these have return type void and that's why
I was getting "the error double cannot be dereenced".

So far, I don't see any other method to use except
getMaximumFractionDigits(int). Then I would need to use
getMaximumIntegerDigits(int) too to get the whole thing. There's got to
be a better way.
 
S

stu

Thomas said:
Maybe you want setMaximumFractionDigits() ?

I realized that but both of these have return type void.

So I tried getMinimumFractionDigits(int) (nad a similar one, i.e
getMaximumDigits(int)for fraction parts) which returns int. Then
concat the two after converting to string? But

using those methods still say "cannot find symbol".
 
M

Mad Bad Rabbit

stu said:
I realized that but both of these have return type void and that's why
I was getting "the error double cannot be dereenced".

So far, I don't see any other method to use except
getMaximumFractionDigits(int). Then I would need to use
getMaximumIntegerDigits(int) too to get the whole thing. There's got to
be a better way.

The Format isn't going to give you a substring with the fraction digits,
it's only set up to give you the entire formatted String. Once you have
that, use indexOf(".") and substring() to get the fraction digits out of
the string.



--
 
S

stu

Mad said:
The Format isn't going to give you a substring with the fraction digits,
it's only set up to give you the entire formatted String.

Actually, that's what I needed.
 
R

Ranganath Kini

Hi,

Please consider using the java.text.DecimalFormat class to format your
decimal output. Here is an example:

import java.text.DecimalFormat;

public class NumberFormat {
public static void main( String[] args ) {
double value = 5.94;
DecimalFormat df = new DecimalFormat( "##.##" );
System.out.println( "The value is: " + df.format( value ) );
}
}

java.text.DecimalFormat class formats the output based on a wildcard
pattern that you feed it. These wild cards are:

# - for a digit, does not show 0 if last
0 - for a digit
.. - for the decimal separator
- - the minus sign
, - grouping separator
E - for scientific notation
% - shows as percentage

And there are many more. Please see the JavaDocs on
java.text.DecimalFormat for more information:

http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

If you want control over the rounding of the numbers during
computations, then consider using the java.math.BigDecimal class.

Hope it helps!
 
N

Noodles Jefferson

stu said:
I tried with setMinimumFractionDigits(int) (from
java.text.NumberFormat) to get 22.22 of 22.222221.

I tried it on the double variable as well as after the double has been
converted to string. What am I doing wrong? Here is my code:

Note that FahrenheitDegree was declared as int.

Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
Double cs = celsiusDegree.setMinimumFractionDigits(2);
String celsiusString = new Double(cs).toString();

//String cstr = celsiusString.setMinimumFractionDigits(2);

String message = String.format(FahrenheitString + " degree Fahrenheit =
%s degree Celsius.", celsiusString);

//String message = String.format(FahrenheitString + " degree Fahrenheit
= %s degree Celsius.", cstr);

import java.text.*;

DecimalFormat df = new DecimalFormat("###,#00.00");

System.out.print(df.format(yourNumber));

--
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Jellyroll" -- Blue Murder

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum,
a pocketknife, and a smile."

-- Robert Redford "Spy Game"
 
S

stu

import java.text.*;

DecimalFormat df = new DecimalFormat("###,#00.00");

System.out.print(df.format(yourNumber));

I found that out yesterday. Unfortunately, I spent so much time for
that little things.
 
S

stu

Ranganath said:
Hi,

Please consider using the java.text.DecimalFormat class to format your
decimal output. Here is an example:

import java.text.DecimalFormat;

public class NumberFormat {
public static void main( String[] args ) {
double value = 5.94;
DecimalFormat df = new DecimalFormat( "##.##" );
System.out.println( "The value is: " + df.format( value ) );
}
}

java.text.DecimalFormat class formats the output based on a wildcard
pattern that you feed it. These wild cards are:

# - for a digit, does not show 0 if last
0 - for a digit
. - for the decimal separator
- - the minus sign
, - grouping separator
E - for scientific notation
% - shows as percentage

And there are many more. Please see the JavaDocs on
java.text.DecimalFormat for more information:

http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

If you want control over the rounding of the numbers during
computations, then consider using the java.math.BigDecimal class.

Thanks for the tips. Does that mean the format mehtod of Decimal Format
does do rounding?
Hope it helps!

Thanks for the detail explanation. I did figure out soon after my last
post.
 
T

Tony Morris

stu said:
I tried with setMinimumFractionDigits(int) (from
java.text.NumberFormat) to get 22.22 of 22.222221.

I tried it on the double variable as well as after the double has been
converted to string. What am I doing wrong? Here is my code:

Note that FahrenheitDegree was declared as int.

Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
Double cs = celsiusDegree.setMinimumFractionDigits(2);
String celsiusString = new Double(cs).toString();

//String cstr = celsiusString.setMinimumFractionDigits(2);

String message = String.format(FahrenheitString + " degree Fahrenheit =
%s degree Celsius.", celsiusString);

//String message = String.format(FahrenheitString + " degree Fahrenheit
= %s degree Celsius.", cstr);

How do I create a String that represents a double (or float) value with only
2 decimal places?

http://jqa.tmorris.net/GetQAndA.action?qids=46&showAnswers=true
 
R

Ranganath Kini

You can use the java.text.DecimalFormat class's format method to
control rounding on the output only.

If you need rounding for numbers during calculations, I strongly
recommend you use java.math.BigDecimal.

Hope it helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top