Search: | |||||

| ||||

Home » Forum » Java | RSS Feeds |

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 ?

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

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.

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

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.

Here is a quick example:

import java.text.DecimalFormat;

class Format{

public static void main(String[] args){

String input = "12345678.2";

DecimalFormat formatter = new

DecimalFormat("#,###,###.00");

String s = formatter.format(

Double.parseDouble(input) );

System.out.println( s );

}

}

the following link have the needede information

java.sun.com/.../decimalFormat.html

u can try this...

new

java.text.DecimalFormat("###,###.##").format(1234567890.123d)

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

Related Topics:

- Number Format
- Formatting number
- Discover number format
- combine number formats
- Converting a "Stringed" Number to a Number
- Converting a "Stringed" Number to a Number
- Convert Serial Number to a Date Function
- Summing on varying number of rows in Excel using VBA
- Number crunching
- find the number of working days (monday to firday) between two give
- Problem with random number in java
- How to control number of textboxes according to user input
- Count unique names in listbox & put the number on the userform
- Detecting text vs numbers with an If/Then
- VBA to use unique password using volume serial number
- Count the number of rows on a worksheet, divide the count by 15
- generating random numbers
- Fibonacci and Prime numbers
- convert from number (0-255) to character
- Math function to determine if a number is an int
- Error preallocating sequence numbers. The sequence table information
- Datagrid Display - right align numbers
- Getting the time for the Number of records retrived
- Number fields in Excel being considered NULL (empty)
- Jbuilder Serial number