【发布时间】:2015-05-21 18:41:56
【问题描述】:
我只想保留点之前的数字,我不想保留之后的数字。例如:
double x = 5.6
int y = 5 // the part before dot
为此,我有以下代码:
SeekBar tipSeekBar;
EditText tipAmountET;
double tipAmount;
tipAmount = (tipSeekBar.getProgress()) * .01;
NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number;
number = format.parse(String.valueOf(tipAmount));
double d = number.doubleValue();
String resultado = String.valueOf(d * 100);
tipAmountET.setText(resultado);
我尝试在resultado 上使用拆分,但它给了我异常ArrayOutOfBoundsException。像这样,在最后一行之前:
String[] split = resultado.split(".");
tipAmountET.setText(split[0]);
【问题讨论】:
-
resultado.split("\\.").... -
双 d = 5.6; int y = (int)d;
-
你可以简单地投射它。将浮点数转换为整数会导致“点之前”的部分(除非超出范围,否则会限制为最大值或最小值)。
标签: java android type-conversion