简体   繁体   English

无法在java中将bigdecimal转换为字符串

[英]not able to convert bigdecimal to string in java

Here is the java code 这是java代码

usageType = (String) c.getSrcValue("USAGETYPE");

c is a arraylist. c是一个arraylist。 I populate it with this field from DB. 我用DB中的这个字段填充它。 "USAGETYPE" NUMBER(*,0) , "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 你能帮帮我吗?

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 (eg CellRenderers in Swing). 通常,作为字符串需要数字的唯一地方是UI,并且您通常有适当的其他地方进行转换(例如,Swing中的CellRenderers)。

Simply write 简单写一下

usageType = c.getSrcValue("USAGETYPE").toString();

or 要么

usageType = ""+c.getSrcValue("USAGETYPE");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM