java - Engineering representation of a BigDecimal instance -


how can configure bigdecimal value if has less 32 characters, should displayed plain string, , otherwise should represented in engeneering format?

for instance:

3690215840369.69874120035964120 - plain string
369043523215840369.69874120035964120 - engineering format of representation

i've tried resolve in following way:

    bigdecimal bd = new bigdecimal("8678679532108467840356746356832624562456786656736.6456442652456345673656");     system.out.println(bd.toengineeringstring());     system.out.println(bd.toplainstring()); 

but outputs same. so, how can gain engineering representation of bigdecimal instance?

couldn't this:

bigdecimal bd = new bigdecimal("8678679532108467840356746356832624562456786656736.6456442652456345673656"); system.out.println(bd.toplainstring().length()<32 ? bd.toplainstring() : bd.toengineeringstring()); 

and if need ignore decimal point when checking 32:

bigdecimal bd = new bigdecimal("8678679532108467840356746356832624562456786656736.6456442652456345673656"); int decimaloffset = 0; if (bd.toplainstring().indexof('.')>=0)         decimaloffset=1; system.out.println(bd.toplainstring().length()-decimaloffset<32 ? bd.toplainstring() : bd.toengineeringstring()); 

but if you're meaning scientific notation, go check out steve's answer here , combine first part of answer replacing bd.toengineeringstring() format(bd)


Comments