Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Number formatting

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Report post to moderator

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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi can u help me out in extending the number format to INR rep.
1,25,000.00 for one lakh, 25 thou...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic