問(wèn)題描述
我有 Hibernate 方法,它返回一個(gè) BigDecimal.我有另一個(gè) API 方法,我需要將該數(shù)字傳遞給它,但它接受 Integer 作為參數(shù).我無(wú)法更改這兩種方法的返回類型或變量類型.
I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods.
現(xiàn)在如何將 BigDecimal 轉(zhuǎn)換為 Integer 并將其傳遞給第二個(gè)方法?
Now how to convert the BigDecimal into Integer and pass it to second method?
有辦法解決嗎?
推薦答案
你會(huì)調(diào)用 myBigDecimal.intValueExact()
(或只是 intValue()
) 如果您丟失信息,它甚至?xí)伋霎惓?這會(huì)返回一個(gè) int,但自動(dòng)裝箱會(huì)處理這個(gè)問(wèn)題.
You would call myBigDecimal.intValueExact()
(or just intValue()
) and it will even throw an exception if you would lose information. That returns an int but autoboxing takes care of that.
這篇關(guān)于將 BigDecimal 轉(zhuǎn)換為 Integer的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!