# Number formatting

Asheesh_talwar
Greenhorn
Posts: 12
I am getting a string with follwing formats
23434
2.32453
223,324,343.34532424
324,343.34532424
2.3453E-01
32,456.9
0
1.0
Note:The Number can be negative also.
First I want to check whether the number is postive or negative
Secondly I want to round the value to two decimal place.
Thirdly I want to display the number in the million format.
Regards
Asheesh

Eric Edwards
Ranch Hand
Posts: 60
Originally posted by Asheesh_talwar:
I am getting a string with follwing formats
23434
2.32453
223,324,343.34532424
324,343.34532424
2.3453E-01
32,456.9
0
1.0
Note:The Number can be negative also.
First I want to check whether the number is postive or negative
Secondly I want to round the value to two decimal place.
Thirdly I want to display the number in the million format.
Regards
Asheesh

Try this!
example:
double x = 134.5678923;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String fx = nf.format(x); // this will print with two decimal places.
java.text has all kinds of number formatting available.

asheesh talwar
Ranch Hand
Posts: 31
Eric my problem is the coma between digits.
The i/p string can be in million format(i.e 234,568.878)
or can be with out it(ie 343445.34) but I have to show it in million format.
Lets Look again what I need
i/p o/p
0 0.00
23.347876 23.35
234,456.02334 234,456.02
234567 234,567.00
2345.456 2,345.46
Note: the number can be negative also.
I am able to solve this by a very lenghty method which I think is a waste in java(which has a ocean of readymade classes).
If any one has a short & to the point answer then it will be of great help.
Thanx
Asheesh

Greenhorn
Posts: 7
hi asheesh
here it the best possible code according to me for the query u have told

import java.text.*;
class Sreplace
{
public static void main(String[] args)
{
String s,s1;
s=args[0];
s1="";
System.out.println(s);
for (int i = 0;i < s.length() ; i++)
{
if (s.charAt(i)!=',')
{
s1 = s1 + s.charAt(i);
}
}
double lvar;
lvar = Double.parseDouble(s1);
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
s1 = nf.format(lvar);
System.out.println(s1);
}
}

convert the above code in proper function and u can use it
presently , u have pass command line argument to get ur output

asheesh talwar
Ranch Hand
Posts: 31
Hi praveen thanx a lot.
I was able to do it by one more method.
DecimalFormat df = new DecimalFormat("#,##0.00");
s1 = df.format(lvar);

Regards
Asheesh

sridhar chenn
Greenhorn
Posts: 2
hi can u help me out in extending the number format to INR rep.
1,25,000.00 for one lakh, 25 thou...

Dirk Schreckmann
Sheriff
Posts: 7023
Welcome to JavaRanch, sridhar!
According to The DecimalFormat Class Documentation:
The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
So, you cannot have varying grouping sizes.

Dirk Schreckmann
Sheriff
Posts: 7023
It looks like this conversation is being continued elsewhere.