StrikeNeo
StrikeNeo

Reputation: 57

Convert Double to specific form

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 ?

Upvotes: 0

Views: 93

Answers (3)

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.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62854

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(".", ""));

Upvotes: 4

brso05
brso05

Reputation: 13222

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);
}

Upvotes: 0

Related Questions