📄 amountconverter.java
字号:
package itso.bank.util;
import itso.bank.exception.InvalidAmountException;
import java.text.NumberFormat;
import java.math.BigDecimal;
/**
* Converts string currency amounts into ints back and forth.
*
* @author Fabio Ferraz
*/
public class AmountConverter
{
public static BigDecimal fromString(String amount) throws InvalidAmountException {
BigDecimal result;
// Try to convert from a string without the currency sign
try {
result = new BigDecimal(amount);
} catch (NumberFormatException e1) {
// Try to convert from a string with the currency sign then
try {
double resultD = NumberFormat
.getCurrencyInstance()
.parse(amount)
.doubleValue();
result = new BigDecimal(resultD);
} catch (Exception e2) {
// Both attempts failed. Throw an exception
throw new InvalidAmountException();
}
}
return result;
}
public static String fromDecimal(java.math.BigDecimal amount) {
return "$"+amount.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -