views:

244

answers:

3

I have a java string which has an dollar value and cents value after decimal points and starting with a + or - sign. I want to convert into cents and store it in a integer (it can have + or -). Also i need to check if the cents part (after decimal point) not more than 2 digits and throw an error message if exists

example : String dollval= "12.23" output value =1223 cents

String dollval= "12" output value = 1200 cents

String dollval= "-0.09" output value = -9 cents

String dollval= ""-99" output value = -99 cents

String dollval= "-99.0" output value = -99 cents

String dollval= "-99.23" output value =-9923 cents

String dollval="0.00 " output value=0 cents

String dollval="5.2" output val= 520 cents

String dollval = "5.20" output value =520 cents

A: 

It's a good homework problem. Try tokenizing (or splitting) on the decimal. The first token will give you your whole dollars. The second part, the pennies. If only 1 character after the decimal, multiply by 10 so that 12.3 becomes 12 * 100 + 3 * 10, not just 3. If more than 2 tokens, error.

You could also do (int) Float.parseFloat(string) * 100, but you might have some rounding problems. Better to stay in integer land.

trenton
Thanks a lot for the info
Arav
+1  A: 

There are still major problems with your examples.

String dollval= "12" output value = 1200 cents

String dollval= "-99" output value = -99 cents

So if a number is positive it is dollars, and if it is negative it is cents? This is illogical.

String dollval= "-99.0" output value = -99 cents

String dollval= "-99.23" output value =-9923 cents

What do the numbers after the '.' actually mean? In the first example they seem to mean a fractional part of a cent. In the second, they mean a fractional part of a dollar. This is illogical.


Having said that, I would implement this using a regex to split the input into parts, convert the parts to integers and then reassemble them based on the meaning inferred from the examples. Something like this:

int cents;
Matcher m = Pattern.compile("([-+]?)([0-9]+)(?:\.([0-9]{0,2}))").matcher(input);
if (m.matches()) {
    int sign = m.group(0).equals("-") ? -1 : 1;
    cents = Integer.parseInt(m.group(1));
    if (m.groupCount() == 3) {
        // I'm assuming that "99.1" should mean 9910 cents ...
        int tmp = (m.group(2).length() == 0) ? 0 :
               (m.group(2).length() == 1) ? (Integer.parseInt(m.group(2)) * 10) :
               Integer.parseInt(m.group(2));
        cents = cents * 100 + tmp;
    }
    cents = cents * sign;
} else {
    throw new SomeException("invalid dollars/cents value");
}

Warning: this code may not even compile, let alone work as expected. And of course, the implementation depends on aspects of the requirements ... which I still think are wrong.

Stephen C
Thanks a lot for your time. I missed the below type of input by the user. Thanks a lot for letting me know "While it you could define "99" to mean 99 cents and "99.0" to mean 9900 cents, that is likely to confuse users"if the user enter "99.1" or "99.10" then it should be 9910 cents. Also there was typo error in the output that i earlier post. I am editing it now
Arav
A: 

I would most definitely not do this with regexps, because that leads to needlessly complex and error-prone code. The Java standard API offers a much simple solution:

    BigDecimal dollars = new BigDecimal(inputString);
    if(dollars.scale()>2)
    {
        throw new IllegalArgumentException();
    }
    int cents = dollars.multiply(new BigDecimal(100)).intValue();

Note though that your requirements above are not consistent:

String dollval= "12" output value = 1200 cents

String dollval= ""-99" output value = -99 cents

My code assumes that this is an error and you meant numbers without a fractional part to be interpreted as full dollars for positive as well as negative numbers.

Michael Borgwardt