java 字符串倍,解析字符串加倍 - java

For a project I have a program that is like a bank. The "bank" reads in a file with accounts in it that look like this:

Miller

William

00001

891692 06

The last string has to be converted to a double so that the program can perform calculations on it like addition and subtraction, etc.

And also I have to print it out in monetary format, i.e. $891692.06

I have this so far:

public class Account {

private String firstName, lastName;

private int accountID;

private String currentBalance;

private static int maxAccountID;

public Account(String fN, String lN, int acct, String bal) {

firstName = fN; lastName = lN;

accountID = acct;

currentBalance = bal;

if(accountID > Account.maxAccountID)

Account.maxAccountID = accountID;

}

public double getBalance(){

String [] s = currentBalance.split(" ");

String balStr = "$"+s[0]+"."+s[1];

double currentBalance = Double.parseDouble(balStr);

return currentBalance;

}

}

Thanks in advance!

解决方案

If your string representing double uses space ' ' instead of a decimal point, you can fix it by replacing the space with a dot, like this:

String currentBalanceStr = "891692 06";

double currentBalanceDbl = Double.parseDouble(currentBalanceStr.replaceAll(" ","."));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值