number conversion

M

mamta81

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?
 
S

Screamin Lord Byron

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?
 
R

Roedy Green

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)
 
R

Roedy Green

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)
 
L

Lew

Roedy said:
mamta81 wrote, quoted or indirectly quoted someone who said :

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.
 
R

Roedy Green

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top