Fortune Telling Collection - Ziwei fortune-telling - How to add and subtract characters and numbers in java

How to add and subtract characters and numbers in java

Convert a string to a number:

There are two ways:

The first kind:

For example; A=" 123 "and b=2, then int num = integer.parseint (a);

Then add and subtract with other numbers;

The second type:

Use bigdecimal num = new bigdecimal (a);

BigDecimal num 2 = new BigDecimal(b);

BigDecimal result = num . add(num 2);

Or subtraction: bigdecimal result = num.subtract (num 2);

Addition, subtraction, multiplication and division can be realized by calling various methods of BigDecimal.

Comparison of two methods:

The first one can only realize integer conversion;

The second one can realize any type of conversion with high calculation accuracy.