无法在java中将bigdecimal转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3488919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
not able to convert bigdecimal to string in java
提问by mujeeb
Here is the java code
这是java代码
usageType = (String) c.getSrcValue("USAGETYPE");
c is a arraylist.
I populate it with this field from DB.
"USAGETYPE" NUMBER(*,0)
,
c 是一个数组列表。我用数据库中的这个字段填充它。
"USAGETYPE" NUMBER(*,0)
,
I get the following error
我收到以下错误
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to String
Can you please help me out
你能帮我吗
回答by Joey
Well, you cannot convert an object to a string by casting. Not in Java, in any case.
好吧,您不能通过强制转换将对象转换为字符串。无论如何,不是在 Java 中。
Try
尝试
usageType = c.getSrcValue("USAGETYPE").toString();
That is, if you actually need it as a string, which smells a little dubious in the first place. Usually the only place where numbers are needed as strings is the UI and you've got appropriate other places to do that conversion, normally (e.g. CellRenderers in Swing).
也就是说,如果你真的需要它作为一个字符串,首先它闻起来有点可疑。通常,唯一需要数字作为字符串的地方是 UI,并且您有适当的其他地方可以进行这种转换,通常(例如 Swing 中的 CellRenderers)。
回答by Tushar Tarkas
Simply write
简单地写
usageType = c.getSrcValue("USAGETYPE").toString();
or
或者
usageType = ""+c.getSrcValue("USAGETYPE");