Java BigDecimal数据转换为相反的符号

时间:2016-09-12 19:55:34

标签: java type-conversion precision bigdecimal truncation

根据Java 7 documentation,类 java.math.BigDecimal 中的方法 longValue 可以返回符号相反的结果。

  

将此BigDecimal转换为long。这种转换类似于Java™语言规范第5.1.3节中定义的从double到short的缩小原语转换:此BigDecimal的任何小数部分都将被丢弃,如果生成的“BigInteger”太大而无法容纳很长,只返回低位64位。请注意,此转换可能会丢失有关此BigDecimal值的整体幅度和精度的信息,以及返回符号相反的结果

在什么情况下可能?

2 个答案:

答案 0 :(得分:5)

只要WITH X AS ( SELECT [JOB_TICKET].[JOB_TICKET_ID], [JOB_TICKET].[REPORT_DATE], [JOB_TICKET].[FIRST_RESPONSE_DATE], [JOB_TICKET].CLOSE_DATE, [JOB_TICKET].LAST_UPDATED, [PRIORITY_TYPE].[PRIORITY_TYPE_NAME] AS 'Ticket_Priority', DATEDIFF(MINute, report_date,FIRST_RESPONSE_DATE) as 'time_to_accept', DATEDIFF(MINUTE,[JOB_TICKET].[FIRST_RESPONSE_DATE],[JOB_TICKET].[CLOSE_DATE]) as 'time_to_resolve' FROM SomeTable ), Y AS ( SELECT X.JOB_TICKET_ID, X.REPORT_DATE, X.FIRST_RESPONSE_DATE, X.CLOSE_DATE, X.LAST_UPDATED, X.Ticket_Priority, X.time_to_accept, X.time_to_resolve, case when time_to_accept <= 10 then 1 else 0 end AS "SLA time_to_accept Status", case when time_to_resolve <= case PRIORITY_TYPE_NAME WHEN 'low' then 960 WHEN 'medium' then 480 WHEN 'high' then 120 WHEN 'Urgent' then 60 END then 1 else 0 end AS "Closed within SLA" ) SELECT Y.JOB_TICKET_ID, Y.REPORT_DATE, Y.FIRST_RESPONSE_DATE, Y.CLOSE_DATE, Y.LAST_UPDATED, Y.Ticket_Priority, Y.time_to_accept, Y.time_to_resolve, Y.[SLA time_to_accept Status], Y.[Closed within SLA], case WHEN [SLA time_to_accept Status]=1 and [Closed within SLA]=1 then 1 else 0 end AS [SLA Compliant] FROM Y 的值大于BigDecimal可以容纳的值,就有可能。

示例:

long

答案 1 :(得分:1)

如果值大于long

的最大值,我将会发生
BigDecimal dec = new BigDecimal(Long.MAX_VALUE +1);
System.out.println(dec.longValue());