2

I am wanting to store an integer named Amount, I want it to be stored in pence so if the user entered 11.45 it would be stored as 1145. What is the best way to remove the decimal point? Should I be using decimalFormatting in Java?

Edit:

It is entered in string format, was going to covert it to an int. I will give one of your solutions ago and let you know if it works but not sure which one would be the best.. Thanks everyone.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3088476
  • 155
  • 1
  • 4
  • 12

4 Answers4

0

times it by 100 and cast as int. Use decimal formatting is double / float are too inaccurate which they may be for money

exussum
  • 18,275
  • 8
  • 32
  • 65
-1

If the user input is in the form of a string (and the format has been verified), then you can strip out the decimal point and interpret the result as an integer (or leave it as a string without the decimal point).

String input = "11.45";
String stripped = input.replace(".", ""); // becomes "1145"
int value = Integer.parseInt(stripped);

If it's a float already, then just multiply by 100 and cast, as @user1281385 suggests.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @MartinMajer - OP said that the input was in a particular format. – Ted Hopp Dec 10 '13 at 20:36
  • "if the user entered 11.45" doesn't mean that he can't enter 11.4500. I think good solution is just to multiply it by 100 and cast to int. – Martin Majer Dec 10 '13 at 20:38
  • @MartinMajer - That requires converting to a float or double first. I was trying to avoid the possible rounding errors that might cost. – Ted Hopp Dec 10 '13 at 20:39
-1

Tested and works. Even if the user enters a number without a decimal, it will keep it as such.

double x = 11.45; // number inputted

String s = String.valueOf(x); // String value of the number inputted
int index = s.indexOf("."); // find where the decimal is located

int amount = (int)x; // intialize it to be the number inputted, in case its an int

if (amount != x) // if the number inputted isn't an int (contains decimal)
    // multiply it by 10 ^ (the number of digits after the decimal place)
    amount = (int)(x * Math.pow(10,(s.length() - 1 - index)));

System.out.print(amount); // output is 1145

// if x was 11.4500, the output is 1145 as well
// if x was 114500, the output is 114500
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • @MartinMajer To have it work for all cases. It's not complicated. Just read the comments. – Michael Yaworski Dec 10 '13 at 20:48
  • It's complicated if you can just multiply the number by 100, round it and convert to int. – Martin Majer Dec 10 '13 at 20:52
  • @MartinMajer That's a horrible solution. It doesn't cover all cases. What if the user enters 1.45? Then what? This isn't complicated. Read the comments and understand. Took me 2 minutes. All the OP has to do is copy and paste and it will work for **all** cases. – Michael Yaworski Dec 10 '13 at 20:53
  • @MartinMajer It's 4 lines of code for the solution. That is definitely not complicated. – Michael Yaworski Dec 10 '13 at 20:54
  • If the user enters 1.45, then multiplying and simple rounding + conversion will return 145, which is correct, I suppose. – Martin Majer Dec 10 '13 at 20:56
  • @MartinMajer lol yeah I meant to provide an example with a different in the fractional numbers. Example is 1.5. This isn't the place for semantics. What I've done is better solution. – Michael Yaworski Dec 10 '13 at 20:57
  • Sorry, but I still don't get it. Give me some examples where the simple solution with multiplying by 100 doesn't work and this one does. – Martin Majer Dec 10 '13 at 21:01
  • @MartinMajer I just said 1.5. Multiply that by 100 and its *150*. Seriously, stop. – Michael Yaworski Dec 10 '13 at 21:02
  • Seriously, the guy was talking about pence and that means money. And 1.5 pounds isn't 15 pence. – Martin Majer Dec 10 '13 at 21:06
  • @MartinMajer You're that immature to -1 my solution *that actually works for all cases* just because he's talking about pence? This solution is generic; yours isn't. If someone enters 11.4, yours will give incorrect output. You shouldn't be on this site if that's the reason you downvote a post. – Michael Yaworski Dec 10 '13 at 21:11
  • If you still think that the piece of code you've written is useful, try it with `1.0000000000001` and `1.000000000000000000001`. You will get some really interesting results. And please read something about floating-point numbers, you just cannot handle them this way. – Martin Majer Dec 10 '13 at 21:24
  • @MartinMajer again with the semantics. If he was worried out numbers being larger than `2^32 - 1`, (ridiculous semantics) then he can make an if statement to not take in a number that large, or just use an even *more complicated* string method. I know how to handle floating-point numbers. My solution is still better than any others on this page. I don't know why it's so hard for you to give someone credit. "Why so complicated?" Really? And you're going into floating-point numbers. – Michael Yaworski Dec 10 '13 at 21:30
  • @mikeyaworski I have used your code(just changed the variable names), but it is asking me to initialise the local variable: -double tranAmount;transactionAmount = String.valueOf(tranAmount);transactionAmount.indexOf("."); int amount = (int)tranAmount; // intialize it to be the number inputted, in case its an int if (amount != tranAmount) // if the number inputted isn't an int (contains decimal) // multiply it by 10 ^ (the number of digits after the decimal place) amount = (int)(tranAmount * Math.pow(10,(transactionAmount.length() - 1 - index))); – user3088476 Dec 13 '13 at 19:02
  • go to this website: http://pastie.org and post your code there. Select `Java` at the top right hand corner and then select `Create Paste`. Copy the link and paste it here. – Michael Yaworski Dec 13 '13 at 19:56
-1

What about convert to float, multiply by 100 and then convert to int?

String pound = "10.45"; // user-entered string
int pence = (int)Math.round(Float.parseFloat(pound) * 100);

This might be also useful: Best way to parseDouble with comma as decimal separator?

Community
  • 1
  • 1
Martin Majer
  • 3,274
  • 4
  • 23
  • 36