Wednesday, September 25, 2013

disable scientific notation in Java

disable scientific notation in Java
To print the number without scientific notation (keep all numbers and without additional 0), although it's ugly, maybe the best way is to
double d = -0.0000000000000000001;
System.out.println(new BigDecimal(Double.toString(d)).stripTrailingZeros().toPlainString());
---0.0000000000000000001
To disable scientific notation globally, can use AOP or dynamic proxy to intercept Number/Decimal 's toString
On Swing JTable, it's controlled by NumberRenderer/DoubleRenderer, can provide custom render or convert it from database side:
Oracle pl/sql
FUNCTION get_orig_value(pi_value IN NUMBER)
RETURN VARCHAR2
IS
BEGIN
  if pi_value = 0 then
    RETURN '0';
  end if;
  if abs(pi_value) < 1 then
    RETURN TO_CHAR(pi_value,'FM0D9999999999999999999999999999999999999999999999999999999999999');
  else
    RETURN TO_CHAR(pi_value,'TM');
  end if;
END get_orig_value;

No comments:

Post a Comment