Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7476
| From | Screamin Lord Byron <scre@min.dot> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: number conversion |
| Date | 2011-08-29 21:35 +0200 |
| Organization | Metronet |
| Message-ID | <j3gplv$j1m$1@news.metronet.hr> (permalink) |
| References | <948475fb-b1da-4062-8008-c64a4ea4206b@x21g2000prd.googlegroups.com> |
On 29.08.2011 20:01, mamta81 wrote:
> when i give checkNumber( 33333335.2534566d); as input i get the
> following o/p
>
> number ------------------->3.33333352534566E7
> n3.33333352534566E7
> Is a decimal number
> index1
> dec 33333352534566E7
> only 6 digits allowed after decimal
> num 3
>
>
> 1) what happens to my input for which I get a wrong index of ". "?
But you *did* get the correct index.
> 2) Is there any other way to find the number of digits before and
> after decimal?
I don't know. You can try by first formatting the String the way you
expect it to be shown, which also includes defining the maximum number
of fraction digits.
For example:
public void checkNumber(double number) {
// UK locale definitely has "." symbol for decimal point
NumberFormat nf = NumberFormat.getInstance(Locale.UK);
nf.setGroupingUsed(false); // don't group digits
nf.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
String numberString = nf.format(number);
String[] nsArray = numberString.split("\\.");
System.out.println("Before decimal point: " + nsArray[0].length());
System.out.println("After decimal point: " + nsArray[1].length());
}
This will work as expected most of the time. :) To see when and why it
won't work as expected, read some literature about floating point numbers.
And now please answer this. Why are you trying to do that? What's the
underlying reason?
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
number conversion mamta81 <roy.mamta@gmail.com> - 2011-08-29 11:01 -0700
Re: number conversion Screamin Lord Byron <scre@min.dot> - 2011-08-29 21:35 +0200
Re: number conversion Roedy Green <see_website@mindprod.com.invalid> - 2011-08-29 14:08 -0700
Re: number conversion Lew <lewbloch@gmail.com> - 2011-08-29 14:17 -0700
Re: number conversion Roedy Green <see_website@mindprod.com.invalid> - 2011-08-29 14:52 -0700
Re: number conversion Roedy Green <see_website@mindprod.com.invalid> - 2011-08-29 14:17 -0700
csiph-web