type conversion - Java BigDecimal data converting to opposite sign long -


according java 7 documentation, method longvalue class java.math.bigdecimal can return result opposite sign.

converts bigdecimal long. conversion analogous narrowing primitive conversion double short defined in section 5.1.3 of java™ language specification: fractional part of bigdecimal discarded, , if resulting "biginteger" big fit in long, low-order 64 bits returned. note conversion can lose information overall magnitude , precision of bigdecimal value return result opposite sign.

in case possible?

it possible whenever value of bigdecimal larger long can hold.

example:

bigdecimal num = new bigdecimal(long.max_value); system.out.println(num);                  // prints: 9223372036854775807 system.out.println(num.longvalue());      // prints: 9223372036854775807  num = num.add(bigdecimal.ten);            // num large long system.out.println(num);                  // prints: 9223372036854775817 system.out.println(num.longvalue());      // prints: -9223372036854775799 system.out.println(num.longvalueexact()); // throws: arithmeticexception: overflow 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -