I am Writing a Android calculator and would like to limit the number of charicters that are outputted to 10
eg:
1: ans = 1.23456789101
should show 1.23456789
2: ans = 1234.56789101
should show 1234.56789
3: ans = 123456789101
should show 1234567890
this is what i have so far but it only works for 1 digit before “.”
ans = (double)Math.round(ans * 100000000) / 100000000;
String output = String.valueOf(ans);
if(output.endsWith(".0")){
output=output.substring(0, output.length()-2);
}
textbox.setText(output);
how would i go about doing this
You could do it with a series of if statements about the value, combined with DecimalFormat
I’m sure there’s a more elegant way of handling this, but this would at least work. You can also, for the values over 10 digits, chain in the else if’s to include some truncating code.