tags:

views:

175

answers:

5

Hi,

As mentioned above. I give one example, let's say all the test values are less than 1 but greater than 0.

  • 0.12 (precision: 3, scale:2)
  • 0.345 (precision: 4, scale:3)
  • 0.6789 (precision: 5, scale:4)

how do i convert those value without hard-coding the scale and precision value.

  • 0.12 -> 12
  • 0.345 -> 345
  • 0.6789 -> 6789

for 0.1 and 0.01 and 0.001 should get 1 (i know this i bad idea but i have been given sets of business rules of the software)

i prefer solution in java but if there is a math algorithm it is better. thanks.

+4  A: 

Multiply by 10 until trunc x = x.

Something like (untested code):

double val = ... ; // set the value.

while(Math.floor(val) != val)
    val *= 10.0;
Charlie Martin
Floating point error *could* theoretically bite you here, since 10 isn't a multiple of 2.
Amber
I am also a little skeptical about this bit: Math.floor(val) != val
Cambium
good idea so far but in term of performance it could be slower for 0.12345678987654321 and more..
ommar
Cambium, that's amenable to proof. I'll leave the details as an exercise, but a real easy way would be by contradiction, ie, assume the contrary.That's skipping Amber's point of floating-point error....
Charlie Martin
actually i like this idea more cuz it is an algorithm
ommar
+3  A: 

One option would be to simply convert to string, split on the decimal point, and grab the portion after it back into an integer:

Integer.parseInt((Double.toString(input_val).split('\\.'))[1])

(Note: you may want to do some error checking as part of the process; the above example code is a condensed version just designed to convey the point.)

Amber
Not a particularly good solution - consider 1.1 vs. 1.001 :)
snemarch
What about it? The OP didn't specify what should be done with leading zeros, if they exist, and it's not as if any other solution could handle them any better...
Amber
the question updated. i know its weird but its documented so
ommar
Problems: will break when input_val < 1E-4; also will yield incorrectly result for certain values (try it with 0.001, that gives you 10)
NullUserException
Can someone tell me the difference between data 0.0012 and 0.12?
eee
@eee: question has been updated, he wants both of those to return 12
snemarch
@NullUserException - could be fixed by using a different type of string conversion, but overall BigDecimal is better anyways.
Amber
A: 

Improving Amber's idea:

//for 0 <= x < 1
Integer.parseInt((Double.toString(x).replaceFirst("0.", ""));

It works well from any value below 1. Any value 1 and above will trigger java.lang.NumberFormatException

Edit: Can someone tell me the difference if the data is like below?

0.0012 -> 12
0.12   -> 12
eee
@NullUserException, I don't see what would be the problem with his code and your example (besides the extra parenthesis at the begining). With x + 0.0000001 the Double.toString(x) would return "0.0000001" and then the replaceFirst would return "0000001" which can be parsed to int right?
Kiranu
Try doing that with x = 0.0000001; also breaks with x = 0.001 (yields 10 instead of 1)
NullUserException
@Kiranu Negatory. `Double.toString` will convert `0.0001` to `1.0E-4`, thus breaking the code.
NullUserException
Yes, there are a lot of problems with this approach...the solution by eugener is better
eee
@NullUserException. Thanks for clearing that
Kiranu
+10  A: 

The solution is much simpler then anybody presented here. You should use BigDecimal:

BigDecimal a = new BigDecimal("0.0000012");
BigDecimal b = a.movePointRight(a.scale());
eugener
+1 for simple and correct answer.
NullUserException
most simplest ever. just asking.. how if x>1 eg: 1.0000012is there any other way?
ommar
+1 but I cannot vote (unregistered)! The BigDecimal(String) constructor should be used for it to work correctly between 0 <= x < 1
eee
Ommar, my method (repeated multiplication) will always work modulo floating point issues.
Charlie Martin
A: 

Are you trying something like this?

public static void main(String[] args) {
    double d = 0.1234;
    while(d>(long)d){
        d*=10;
    }
    System.out.println((long)d);
}
demotics2002