将字符串转换为bigdecimal时出错

时间:2017-07-12 07:12:28

标签: java parsing bigdecimal

早安, 我将字符串解析为大小数,但是当我将模式ans simbols设置为它时,调试器会返回错误。代码非常简单,我已从文档和stackoverflow上的其他帖子中获取它,我不记得它是什么地址。它在下面:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setGroupingSeparator('.');
    symbols.setDecimalSeparator(',');
    String pattern = "#.##0,0#";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
    decimalFormat.setParseBigDecimal(true);

(BigDecimal) decimalFormat.parse(entity.getQta()))

此行引发错误:

DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);

GWT调试器的StackTrace:

com.smartgwt.client.core.JsObject$SGWT_WARN: 

09:29:12.673:MOU6:WARN:RPCManager:ATTENZIONE: si e' verificato un errore imprevisto [SC: 500]undefined - response: {clientContext: Obj,
status: -1,
invalidateCache: true,
data: "ATTENZIONE: si e' verificato un errore i..."[59],
internalClientContext: Obj,
context: undef,
startRow: 0,
endRow: 0,
totalRows: 0}   at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)   at  
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)    at 
 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   at 
 java.lang.reflect.Constructor.newInstance(Constructor.java:422)    at 
 com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)  at 
 com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)     at 
 com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)  at 
  com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)     at 
 com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)  at 
  com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)     at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)  at 
com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304)     at 
 com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)  at com.smartgwt.client.data.DataSource.processResponse(DataSource.java)     at 
com.sgss.common.client.ds.SgssGwtRpcDataSource.handleFailure(SgssGwtRpcDataSource.java:205)     at 
 com.sgss.common.client.ds.SgssGwtRpcDataSource$GenericAsyncCallback.onFailure(SgssGwtRpcDataSource.java:148)   at 
 com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:237)     at 
com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)     at 
com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)     at sun.reflect.GeneratedMethodAccessor417.invoke(Unknown Source)    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   at java.lang.reflect.Method.invoke(Method.java:497)     at 
 com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)  at 
 com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)     at 
 com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)  at 
 com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)  at 
 com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)  at 
 com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)  at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)  at 
 com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)  at 
 com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)     at com.google.gwt.core.client.impl.Impl.apply(Impl.java)    at 
 com.google.gwt.core.client.impl.Impl.entry0(Impl.java:356)     at 
 sun.reflect.GeneratedMethodAccessor412.invoke(Unknown Source)  at 
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  at java.lang.reflect.Method.invoke(Method.java:497)     at 
  com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)     at 
 com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)     at 
 com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)  at 
 com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)   at 
  com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)    at 
 com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)   at java.lang.Thread.run(Thread.java:745)
编辑:我没有提到GWT(在我的情况下是SMartGwt,因为我在后端进行了这种解析,它不使用smartgwt但只使用spring和hibernate。

1 个答案:

答案 0 :(得分:2)

您的模式不正确,在以下行中:

String pattern = "#.##0,0#";

根据文件 https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html(在标题为特殊模式字符的部分)","在模式中签名是分组分隔符的储备 - 不是数字中的逗号; "。"保留给小数点分隔符,而不是字面上的点字符。

由于您的数字格式为1.743.711,67(而非1,743,711.67),因此您使用符号变量来指定","作为小数点分隔符和"。"作为分组分隔符。但是,该模式并不遵循相同的逻辑。它交换分组和小数分隔符的位置,使其失败。

我建议你试试:

String pattern = "#,##0.0#";

使用相同的代码,看看它是否有帮助。