問題描述
我有 Hibernate 方法,它返回一個 BigDecimal.我有另一個 API 方法,我需要將該數字傳遞給它,但它接受 Integer 作為參數.我無法更改這兩種方法的返回類型或變量類型.
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.
現在如何將 BigDecimal 轉換為 Integer 并將其傳遞給第二個方法?
Now how to convert the BigDecimal into Integer and pass it to second method?
有辦法解決嗎?
推薦答案
你會調用 myBigDecimal.intValueExact()
(或只是 intValue()
) 如果您丟失信息,它甚至會拋出異常.這會返回一個 int,但自動裝箱會處理這個問題.
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.
這篇關于將 BigDecimal 轉換為 Integer的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!