0

I have this code below:

System.out.println(transaction.getAmount());

that returns the amount in this form: 100.0 (which is double).

How can I convert it into this form: 10000 ?

4
  • 1
    System.out.println((int)(transaction.getAmount() * 100)); Commented Nov 21, 2014 at 14:34
  • 1
    Are you sure you know what you want? 100.0 != 10000 Commented Nov 21, 2014 at 14:35
  • 1
    Do you mean 100.00 ? Commented Nov 21, 2014 at 14:35
  • i need to remove the point to use this in my JSON code. basically my system retrieve a value of 100.00=100.00 US$, but i need to put the value as 10000 into this JSON in order for it to read it as 100 US$ Commented Nov 21, 2014 at 14:41

3 Answers 3

4

Multiply by 100 and get the integer value:

 System.out.println(Double.valueOf(transaction.getAmount() * 100).intValue());

Another option is to use a String.format()

double amount = transaction.getAmount();
System.out.println(String.format("%1$,.2f", amount).replace(".", ""));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot kocko, this work System.out.println(Double.valueOf(transaction.getAmount() * 100).intValue());
0

You could use system.out.format and multiply your number by 100:

public static void main(String args[]) {
   double test = 100;
   test = test * 100;
   System.out.format("Here is my number: %.0f", test);
}

Comments

0

This is how you do it:

int x;
double y = transaction.getAmount() * 100.00;
x = (int)y;
System.out.println(x);

See? You do explicit type casting to get your result in int, because double is a larger data-type than int.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.