使用Codename One格式化十进制数

时间:2017-03-18 07:47:46

标签: codenameone

Codename One不允许我使用DecimalFormat。那么我该如何解决这段代码:

private final static DecimalFormat balanceFormatter = new DecimalFormat("0.00");

private final String GetWalletBalanceEndpoint = "wallet/balance";
public GetWalletBalance() {
    super();
    endpoint = GetWalletBalanceEndpoint;
}

public final Double getBalance() {
    Map<String, Object> balanceMap = getResponseData();
    Double balance = (Double) balanceMap.get("balance");
    return balance;
}


public String getBalance2DecimalsString() {
    Double balance = getBalance();
    return balanceFormatter.format(balance);
}

1 个答案:

答案 0 :(得分:2)

您可以使用L10NManager

在Codename One中格式化小数
private final L10NManager lnm = L10NManager.getInstance();

private final String GetWalletBalanceEndpoint = "wallet/balance";
public GetWalletBalance() {
    super();
    endpoint = GetWalletBalanceEndpoint;
}

public final String getBalance() {
    Map<String, Object> balanceMap = getResponseData();
    return lnm.format(Double.parseDouble(balanceMap.get("balance").toString), 2);
}

注意将根据设备本地化格式化值。有些国家/地区将10.34写为10,34,将遵循此格式。您可以通过在格式化之前设置本地化来强制使用特定国家/地区的格式:

lnm.setLocale("US", "EN");