-1

In my app there are values like below:

double a = 999999999;
double b = 9999999999999999f;
double c = 9876543210987.654321f;

Not always but sometimes. When i try to show them in textview, their low value figures changes and show different number. I try many ways to show them in textview true but not successed. Below is all my ways:

double a = 999999999;
double b = 9999999999999999f;
double c = 9876543210987.654321f;

DecimalFormat formatter = new DecimalFormat("#");
TextView tv = findViewById(R.id.tv);

tv.setText(
    String.format("a: %f" , a) + "\n" +
    String.format("b: %f" , b) + "\n" +
    String.format("c: %f" , c) + "\n" +
    "float to string a: " + Double.valueOf(a) + "\n" +
    "float to string b: " + Double.valueOf(b) + "\n" +
    "float to string c: " + Double.valueOf(c) + "\n" +
    "cast a: " + (long)a + "\n" +
    "cast b: " + (long)b + "\n" +
    "cast c: " + (long)c + "\n" +
    "math round a: " + Math.round(a) + "\n" +
    "math round b: " + Math.round(b) + "\n" +
    "math round c: " + Math.round(c) + "\n" +
    "big decimal a: " + new BigDecimal(a).toPlainString() + "\n" +
    "big decimal b: " + new BigDecimal(b).toPlainString() + "\n" +
    "big decimal c: " + new BigDecimal(c).toPlainString() + "\n" +
    "formatted a: " + formatter.format(a) + "\n" +
    "formatted b: " + formatter.format(b) + "\n" +
    "formatted c: " + formatter.format(c) + "\n"
);

And below is the output:

a: 999999999.000000
b: 10000000272564200.000000
c: 9876543635456.000000
float to string a: 9.99999999E8
float to string b: 1.0000000272564224E16
float to string c: 9.876543635456E12
cast a: 999999999
cast b: 10000000272564224
cast c: 9876543635456
math round a: 999999999
math round b: 10000000272564224
math round c: 9876543635456
big decimal a: 999999999
big decimal b: 10000000272564224
big decimal c: 9876543635456
formatted a: 999999999
formatted b: 10000000272564200
formatted c: 9876543635456
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
hamed
  • 148
  • 11

1 Answers1

4

If you convert to Strings first you can do as

    System.out.println("big decimal a: " + new BigDecimal("999999999"));
    System.out.println("big decimal b: " + new BigDecimal("9999999999999999"));
    System.out.println("big decimal c: " + new BigDecimal("9876543210987.654321"));

output

big decimal a: 999999999
big decimal b: 9999999999999999
big decimal c: 9876543210987.654321

NB - hard-coded for ease of reading

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • your code was worked but i tryed this one: ```Log.d("MESSAGE", "big decimal b: " + new BigDecimal(String.valueOf(b)));``` and output was: ```D/MESSAGE: big decimal b: 1.0E+16``` – hamed Jan 14 '20 at 07:30
  • see this [answer](https://stackoverflow.com/a/8907745/2310289) – Scary Wombat Jan 14 '20 at 07:49