Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 把货币四舍五入到最接近的美元,你是怎么做到的?_Android_Exception_Currency_Rounding - Fatal编程技术网

Android 把货币四舍五入到最接近的美元,你是怎么做到的?

Android 把货币四舍五入到最接近的美元,你是怎么做到的?,android,exception,currency,rounding,Android,Exception,Currency,Rounding,除了运行4.1.2的Galaxy Nexus之外,以下代码适用于我的生产应用程序中的所有手机。Play store向我发送了错误,我将首先发布代码,然后发布错误: // Roughly formats the currency by loping off the decimal places (amount // comes already rounded) public static String formatCurrencyRound(double amount) { Str

除了运行4.1.2的Galaxy Nexus之外,以下代码适用于我的生产应用程序中的所有手机。Play store向我发送了错误,我将首先发布代码,然后发布错误:

    // Roughly formats the currency by loping off the decimal places (amount
// comes already rounded)
public static String formatCurrencyRound(double amount) {
    String currency = formatCurrency(amount); // Returns "$8.00"

    return currency.substring(0, currency.indexOf("."));
}
错误:

java.lang.RuntimeException: Unable to resume activity {com.ootpapps.saving.made.simple/com.ootpapps.saving.made.simple.SavingsPlanOverview}: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-1
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-1
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.ootpapps.saving.made.simple.App.formatCurrencyRound(App.java:68)
at com.ootpapps.saving.made.simple.SavingsPlanOverview.updateSavingsPlanWidgets(SavingsPlanOverview.java:224)
at com.ootpapps.saving.made.simple.SavingsPlanOverview.onResume(SavingsPlanOverview.java:156)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
... 12 more
如果有人能提出一种更好的方法来对这个货币字符串进行四舍五入,或者,一种更好的方法来将原始数字四舍五入到一个没有小数的格式化字符串中,我将不胜感激。同样,由于某种原因,这只会在运行4.1.2的GNexus上失败

谢谢

编辑添加的格式货币代码:

public static String formatCurrency(double amount) {
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    return currency.format(amount);
}
编辑cont'd-我使用的代码,该代码用于将货币四舍五入为整美元

public static String formatCurrencyWhole(double amount) {
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    currency.setMaximumFractionDigits(0);
    currency.setMinimumFractionDigits(0);

    return currency.format(amount);
}

首先,货币不应该,永远,ever使用
double
s来处理。您应该使用
BigDecimal
来保持精度。(有关详细信息,请参阅。)

除此之外,您还可以使用
NumberFormat
获取您的
字符串
表示:

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String result = format.format(amount);

采取双重;如果最终使用的是
BigDecimal
,请使用
BigDecimal.doubleValue()
。提供此区域的一些附加详细信息。

首先,货币不应使用
double
s处理。您应该使用
BigDecimal
来保持精度。(有关详细信息,请参阅。)

除此之外,您还可以使用
NumberFormat
获取您的
字符串
表示:

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String result = format.format(amount);

采取双重;如果最终使用的是
BigDecimal
,请使用
BigDecimal.doubleValue()
。在这方面提供了一些额外的细节。

我认为字符串的格式已经是$8了

如果小数点不存在:

currency.substring(0,currency.indexOf(“.”)

将抛出IndexOutOfBoundsException

解决方案可能很简单,只需事先测试十进制数即可

公共静态字符串格式CurrencyRound(双倍金额){ 字符串currency=formatCurrency(amount);//返回“$8.00”

}

-------编辑----------

正如Ken(非常聪明的家伙)在评论中指出的,堆栈跟踪中的关键行是:

java.lang.StringIndexOutOfBoundsException:length=9;regionStart=0;区域长度=-1 位于java.lang.String.StartAndLength(String.java:593)

此错误显示regionLength=-1,这意味着indexOf(“.”)的结果为-1。((“)不存在)

它还显示了长度为9的字符串,这使我认为不同的分隔符问题(Ken也指出)可能是真正的问题。除非美元金额在10000000美元范围内

这就是为什么我希望看到formatCurrency(Double)的代码

-----编辑-----

如有疑问,您正在使用默认区域设置:

NumberFormat currency=NumberFormat.getCurrencyInstance()

文件中有这样一句话:

“默认区域设置不适用于机器可读的输出。最好的选择通常是locale.US–此区域设置保证在所有设备上都可用,而且它没有令人惊讶的特殊情况,并且经常使用(特别是在计算机通信中)这意味着它往往也是最有效的选择

一个常见的错误是,在生成机器可读的输出时隐式使用默认区域设置。这通常适用于开发人员的测试设备(特别是因为如此多的开发人员使用en_US),但在用户处于更复杂区域设置的设备上运行时失败。”

除非您的应用程序需要外币,否则请使用:


NumberFormat currency=NumberFormat.getCurrencyInstance(Locale.US)

我想字符串的格式已经是8美元了

如果小数点不存在:

currency.substring(0,currency.indexOf(“.”)

将抛出IndexOutOfBoundsException

解决方案可能很简单,只需事先测试十进制数即可

公共静态字符串格式CurrencyRound(双倍金额){ 字符串currency=formatCurrency(amount);//返回“$8.00”

}

-------编辑----------

正如Ken(非常聪明的家伙)在评论中指出的,堆栈跟踪中的关键行是:

java.lang.StringIndexOutOfBoundsException:length=9;regionStart=0;区域长度=-1 位于java.lang.String.StartAndLength(String.java:593)

此错误显示regionLength=-1,这意味着indexOf(“.”)的结果为-1。((“)不存在)

它还显示了长度为9的字符串,这使我认为不同的分隔符问题(Ken也指出)可能是真正的问题。除非美元金额在10000000美元范围内

这就是为什么我希望看到formatCurrency(Double)的代码

-----编辑-----

如有疑问,您正在使用默认区域设置:

NumberFormat currency=NumberFormat.getCurrencyInstance()

文件中有这样一句话:

“默认区域设置不适用于机器可读的输出。最好的选择通常是locale.US–此区域设置保证在所有设备上都可用,而且它没有令人惊讶的特殊情况,并且经常使用(特别是在计算机通信中)这意味着它往往也是最有效的选择

一个常见的错误是,在生成机器可读的输出时隐式使用默认区域设置。这通常适用于开发人员的测试设备(特别是因为如此多的开发人员使用en_US),但在用户处于更复杂区域设置的设备上运行时失败。”

除非您的应用程序需要外币,否则请使用:


NumberFormat currency=NumberFormat.getCurrencyInstance(Locale.US)

如果你还想缩小,你是否考虑了数学?SoRoR()?你能把格式化货币(double)的代码发布吗?最终:我会用NoMyFrad格式,简单地将StutoDigiTi设置为max和min为0。我不知道这件事