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);
Mark Thornton - 05 Feb 2006 18:45 GMT
> I tried with setMinimumFractionDigits(int) (from
> java.text.NumberFormat) to get 22.22 of 22.222221.
[quoted text clipped - 15 lines]
> //String message = String.format(FahrenheitString + " degree Fahrenheit
> = %s degree Celsius.", cstr);
setMaximumFractionDigits!
stu - 05 Feb 2006 18:56 GMT
> 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.
Mad Bad Rabbit - 05 Feb 2006 19:18 GMT
>> setMaximumFractionDigits!
>
[quoted text clipped - 5 lines]
> 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.
>;k
stu - 06 Feb 2006 04:10 GMT
> >> setMaximumFractionDigits!
> >
[quoted text clipped - 8 lines]
> 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.
>Once you have
> that, use indexOf(".") and substring() to get the fraction digits out of
> the string.
> >;k
Ranganath Kini - 06 Feb 2006 07:14 GMT
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!
stu - 06 Feb 2006 22:53 GMT
> Hi,
>
[quoted text clipped - 29 lines]
> 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.
Ranganath Kini - 07 Feb 2006 06:04 GMT
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!
stu - 05 Feb 2006 18:50 GMT
I should use setMaximumFractionDigits(int) but the same error anyway:
double cannot be dereferenced.
Thomas Schodt - 05 Feb 2006 18:51 GMT
> I tried with setMinimumFractionDigits(int) (from
> java.text.NumberFormat) to get 22.22 of 22.222221.
Maybe you want setMaximumFractionDigits() ?
stu - 05 Feb 2006 19:12 GMT
> > I tried with setMinimumFractionDigits(int) (from
> > java.text.NumberFormat) to get 22.22 of 22.222221.
>
> 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".
Noodles Jefferson - 06 Feb 2006 08:12 GMT
> I tried with setMinimumFractionDigits(int) (from
> java.text.NumberFormat) to get 22.22 of 22.222221.
[quoted text clipped - 15 lines]
> //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));

Signature
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"
stu - 06 Feb 2006 22:49 GMT
> 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.
Tony Morris - 07 Feb 2006 02:01 GMT
> I tried with setMinimumFractionDigits(int) (from
> java.text.NumberFormat) to get 22.22 of 22.222221.
[quoted text clipped - 15 lines]
> //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

Signature
Tony Morris
http://tmorris.net/