Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Formatting a Number

  Asked By: Willie    Date: Jan 27    Category: Java    Views: 1741

I'd like to format a Number, say, a float or a double, as a String in
the Format 999,999.99 and then display it.

How can this be done ?



7 Answers Found

Answer #1    Answered By: Al Allen     Answered On: Jan 27

yes it cain it is double.tostring("the number")

Answer #2    Answered By: Viola Hanson     Answered On: Jan 27

Thanks for the reply.

toString returns a default String. As far as I am aware, it does not
provide the flexibility to format  this String in a programmer-
specifiable manner.

I really wanted to ask if there is a ready-made method to format a
number in a programmer-specifiable manner.

Answer #3    Answered By: Arthur Cole     Answered On: Jan 27

You can use the java.text package. It provides a variety of formats.

Answer #4    Answered By: Jim Williamson     Answered On: Jan 27

You should use the class java.text.DecimalFormat. I haven't used it
myself, so I can't help you much more, but this is probably the class
you need.

Answer #5    Answered By: Sherri Parker     Answered On: Jan 27

Here is a quick example:

import java.text.DecimalFormat;

class Format{
public static void main(String[] args){
String input = "12345678.2";
DecimalFormat formatter = new
String s = formatter.format(
Double.parseDouble(input) );
System.out.println( s );

Answer #6    Answered By: Rachel Barnes     Answered On: Jan 27

the following link have the needede information


Answer #7    Answered By: Julio Morgan     Answered On: Jan 27

u can try this...


Didn't find what you were looking for? Find more on Formatting a Number Or get search suggestion and latest updates.