double.tostring()方法
时间: 2023-05-02 18:01:57 浏览: 106
double.tostring()是一个在Python中用于将双精度浮点数转换为字符串的方法。该方法可以将一个双精度浮点数转换为科学计数法或定点数表示的字符串,并可以指定保留的小数位数、舍入方式等参数。
相关问题
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1)); java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue();将结果改为保留4位小数
好的,您可以使用以下代码来将结果保留4位小数:
```
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
double result = b1.multiply(b2).doubleValue();
result = (double)Math.round(result * 10000) / 10000;
return result;
```
其中,`result`变量的值即为两个数相乘的结果,经过四舍五入后保留4位小数。
if (button.equals("C")) { // clear txtResult.setText(""); num1 = 0; num2 = 0; op = ""; } else if (button.equals("+")) { // addition num1 = Double.parseDouble(txtResult.getText()); op = "+"; txtResult.setText(""); } else if (button.equals("-")) { // subtraction num1 = Double.parseDouble(txtResult.getText()); op = "-"; txtResult.setText(""); } else if (button.equals("*")) { // multiplication num1 = Double.parseDouble(txtResult.getText()); op = "*"; txtResult.setText(""); } else if (button.equals("/")) { // division num1 = Double.parseDouble(txtResult.getText()); op = "/"; txtResult.setText(""); } else if (button.equals("=")) { // equals num2 = Double.parseDouble(txtResult.getText()); if (op.equals("+")) { txtResult.setText(Double.toString(num1 + num2)); } else if (op.equals("-")) { txtResult.setText(Double.toString(num1 - num2)); } else if (op.equals("*")) { txtResult.setText(Double.toString(num1 * num2)); } else if (op.equals("/")) { txtResult.setText(Double.toString(num1 / num2)); } } else { // digit txtResult.setText(txtResult.getText() + button); }改用switch语句
可以将上述代码改写为使用 switch 语句的形式,代码如下所示:
```
switch (button) {
case "C":
txtResult.setText("");
num1 = 0;
num2 = 0;
op = "";
break;
case "+":
num1 = Double.parseDouble(txtResult.getText());
op = "+";
txtResult.setText("");
break;
case "-":
num1 = Double.parseDouble(txtResult.getText());
op = "-";
txtResult.setText("");
break;
case "*":
num1 = Double.parseDouble(txtResult.getText());
op = "*";
txtResult.setText("");
break;
case "/":
num1 = Double.parseDouble(txtResult.getText());
op = "/";
txtResult.setText("");
break;
case "=":
num2 = Double.parseDouble(txtResult.getText());
switch (op) {
case "+":
txtResult.setText(Double.toString(num1 + num2));
break;
case "-":
txtResult.setText(Double.toString(num1 - num2));
break;
case "*":
txtResult.setText(Double.toString(num1 * num2));
break;
case "/":
txtResult.setText(Double.toString(num1 / num2));
break;
}
break;
default:
txtResult.setText(txtResult.getText() + button);
break;
}
```
使用 switch 语句可以使代码更加简洁易读,并且能够避免出现多个 if 语句嵌套的情况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)