formatter - How to add decimal point before x digits in Java -
lets suppose have value 12345678 , number x=2, , want final output 123456.78 , if value of x 4, final output 1234.5678.
please tell how can achieve this?
given you're dealing shifting decimal point, i'd use bigdecimal
:
long integral = 12345678l; int x = 4; // or 2, or whatever bigdecimal unscaled = new bigdecimal(integral); bigdecimal scaled = unscaled.scalebypoweroften(-x); system.out.println(scaled); // 1234.5678
Comments
Post a Comment