Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Number Formatting of a doiuble to 2 decimal position

Reply
Thread Tools

Number Formatting of a doiuble to 2 decimal position

 
 
stu
Guest
Posts: n/a
 
      02-06-2006

>
> 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.

 
Reply With Quote
 
 
 
 
stu
Guest
Posts: n/a
 
      02-06-2006

Ranganath Kini wrote:
> 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/...malFormat.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.

 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      02-07-2006
"stu" <> wrote in message
news: ups.com...
> 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.acti...owAnswers=true

--
Tony Morris
http://tmorris.net/


 
Reply With Quote
 
Ranganath Kini
Guest
Posts: n/a
 
      02-07-2006
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal.Decimal formatting python@lists.fastmail.net Python 0 07-19-2010 12:32 PM
convert decimal number in a hexadecimal number ? muss C Programming 13 03-27-2006 08:14 AM
Formatting decimal number Luis Esteban Valencia ASP .Net 1 01-12-2005 02:13 PM
Formatting a number, placing the decimal in the proper place BoomerangThree Java 3 08-20-2004 05:49 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57