java - Java将字符串转换为双精度
问题描述
我创建了一个基本的 GUI,它获取用户输入的金额并用预先存在的金额减去它。但是它不会让我运行下面的那个,因为我得到一个错误,字符串不能转换为双精度。使用 parseInt 方法在这里不起作用,因为我使用 getText 方法。
String message = txtaMessage.getText();
String transaction1 = txtfAmountTransfer.getText();
String reciever = txtfTransferTo.getText();
lblOutputTransferInfo.setText("Your Amount of "+transaction1+" has been sent to "+reciever+" .");
lblOutputTransferMsg.setText("With a Message: "+ message);
double balance = 5123.84;
String newBalance = balance - transaction1;//this will not work but the concept I need
lblSavingsBalance.setText(newBalance);
解决方案
它不起作用,因为这里的 transcation1 是一个字符串,而 balance 是双倍的。在java中,我们没有从double中减去字符串的方法。所以,首先,你需要转换transaction1
成双精度。我建议你
Double newBalance = balance - Double.parseDouble(transcation1);
然后lblSavingsBalance.setText(newBalance.toString());
将newBalance
双精度值转换为字符串
希望它工作正常...
推荐阅读
- apache-spark - Spark Dataframe 中的嵌套数据透视表
- angular7 - Angular http 获取 Date 属性的错误数据类型
- python - 为什么在将 LAB 空间图像写入目录时出现黑色图像?
- c# - c# GUI 在启动控制台应用程序后冻结
- java - android Draw Text with text size change and translate in canvas
- sql - SQL 查询以选择记录,该记录检查至少一条具有给定条件的有效记录
- java - 我需要明确插入弹簧核心模块吗?
- python-3.x - 在 tensorflow 1.12 中读取 uint16 图像的奇怪问题
- php - WooCommerce:在产品列表视图中添加新列并使其可排序
- java - 以编程方式加载弹簧配置