java - BigDecimal initialized with integer literal with leading zeros -


this question has answer here:

kindly me in understanding following code,

    bigdecimal d = new bigdecimal(000100);     system.out.println(d); // output 64!!!!      bigdecimal x = new bigdecimal(000100.0);     system.out.println(x); // output 100 

shouldn't use bigdecimal process int or long value in scenario? (i mean leave performance , stuff, know not advisable use bigdecimal process int or long). data has mix of both long , decimal values, trying know bigdecimal.

the problem not bigdecimal literal numbers you're passing in. when int literal starts 0, java interprets octal number.

an octal numeral consists of ascii digit 0 followed 1 or more of ascii digits 0 through 7 interspersed underscores, , can represent positive, zero, or negative integer.

that why 000100 produces 64 -- 1008 64 in decimal.

decimal literals don't have leading zeros, don't use any.

bigdecimal d = new bigdecimal(100); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -