Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > number conversion

Reply
Thread Tools

number conversion

 
 
mamta81
Guest
Posts: n/a
 
      08-29-2011
Hi all,

I have written a number check function. The purpose is to know number
of digits before and after decimal.


public void checkNumber(double number){

System.out.println("number ------------------->" + number );
Double d = new Double(number);
String n = d.toString();
System.out.println("n" + n);

for(int i =0; i< n.length();i++){
if(n.charAt(i)=='.'){
System.out.println("Is a decimal number");
}
}

int index = n.indexOf(".");
System.out.println("index" + index);
String dec = n.substring(index + 1);
System.out.println("dec " + dec);

if(dec.length() > 6 ){
System.out.println("only 6 digits allowed after decimal");
}

String num = n.substring(0,n.indexOf("."));
System.out.println("num " + num);
if(num.length()>10){
System.out.println("Only 10 places allowed before decimal");
}
}
}


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 ". "?
2) Is there any other way to find the number of digits before and
after decimal?
 
Reply With Quote
 
 
 
 
Screamin Lord Byron
Guest
Posts: n/a
 
      08-29-2011
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?
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-29-2011
On Mon, 29 Aug 2011 11:01:44 -0700 (PDT), mamta81
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>1) what happens to my input for which I get a wrong index of ". "?


Java starts numbering slots at 0 not 1. Your program is correct.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-29-2011
On Mon, 29 Aug 2011 11:01:44 -0700 (PDT), mamta81
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>2) Is there any other way to find the number of digits before and
>after decimal?


Your code could be simplified by using

final int place = s.indexof( '.' );

n is traditionally a count, not a String. This dates back to FORTRAN
days.

Since the number you are looking at is expressed in binary, it really
does not have a number of decimal places. You can choose any number
of places you please in the string representation if you use
DecimalFormat. You are just measuring an artifact of Java's default
double -> String conversion method.
See http://mindprod.com/applet/converter.html

For some completely different techniques see:
http://mindprod.com/jgloss/widthindigits.html
http://mindprod.com/jgloss/log.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-29-2011
Roedy Green wrote:
> mamta81 wrote, quoted or indirectly quoted someone who said :
>> 1) what happens to my input for which I get a wrong index of ". "?

>
> Java starts numbering slots at 0 not 1. Your program is correct.


Well, not really correct at all, but you are getting the correct index.

Quoting the OP's code:

>


Incomplete example. This code will not compile.

> public void checkNumber(double number){
>
> System.out.println("number ------------------->" + number );


Space missing after label.

> Double d = new Double(number);


Why do you bother creating an instance of 'Double'?

> String n = d.toString();


Use of the default formatting and expecting different results.

> System.out.println("n" + n);


Space missing after label.

> for(int i =0; i< n.length();i++){


Explicit loop instead of standard API call. Recomputation of 'n.length()' in every loop pass.

> if(n.charAt(i)=='.'){


Expensive call to 'charAt()' in lieu of iteration through code points, or at least the char sequence.

> System.out.println("Is a decimal number");


Space missing before label.

> }


No indentation.

> }
>
> int index = n.indexOf(".");


Redundant use of the API call after having already found the value the hard way. Why calculate the value twice, much less two completely different ways?

> System.out.println("index" + index);


Space missing after label.

> String dec = n.substring(index + 1);
> System.out.println("dec " + dec);
>
> if(dec.length() > 6 ){


Magic value '6'. Why that value? No comments. Who says 6 is the right number?

> System.out.println("only 6 digits allowed after decimal");
> }
>
> String num = n.substring(0,n.indexOf("."));


Third recomputation of the same value.

> System.out.println("num " + num);
> if(num.length()>10){


Magic value '10'. Who says 10 is the right number?

> System.out.println("Only 10 places allowed before decimal");
> }
> }
> }


Constants should at minimum be pulled into (static) final variables.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-29-2011
On Mon, 29 Aug 2011 14:17:31 -0700 (PDT), Lew <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Well, not really correct at all, but you are getting the correct index.


exactly. I did not have the patience to do what you did.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Conversion from Hexadecimal number into Decimal number dharmdeep@gmail.com C Programming 14 11-16-2006 07:51 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM



Advertisments