在hibernate查询语言中舍入到2个小数位 [英] Round to 2 decimal places in hibernate query language

查看:307
本文介绍了在hibernate查询语言中舍入到2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好可以帮助我如何在hql中舍入到2个小数位吗?

我找不到任何联机。以下是我的查询:

 选择p.amount作为amt,p.desc from pay p,register r其中r.type = ?1和r.code =?2; 

如果有人能为此提供帮助,我会很高兴。



使用的技术:hibernate,spring,java,primefaces 4.0,oracle数据库

解决方案

很多人终于找到了以下解决方案:

如果您希望更独立于数据库,并且特别想支持Postgres或Oracle DB,则可以使用floor函数进行舍入,即非常通用,不会为不同的实现留出太多空间,这意味着这可能适用于所有数据库(使用Postgres和MySQL进行测试)。



该代码段执行舍入至2位数并以绝对值进行舍入。你可以很容易地适应你的需求。



关于使用100.0的100除法。这将确保你总是得到一个双倍的结果,这是非常重要的,如果你建立一个总和四舍五入的价值。



floor(abs (value)* 100 + 0.5)/100.0 * sign(value)

Hi can someone help me how to round to 2 decimal places in hql?
I can't find anything online. Below is my query:

Select p.amount as amt,p.desc from pay p, register r where r.type=?1 and r.code=?2;

I would be glad if someone can help on this.

Technology used: hibernate, spring, java, primefaces 4.0, oracle database

解决方案

I have struggled with this a lot an finally found the following solution:

If you want to be more DB independent and specifically want to support Postgres or Oracle DB you can use the floor function for rounding which is pretty generic and does not leave much room for different implementations, meaning this will probably work with all databases (tested with Postgres and MySQL).

The snippet performs rounding to 2 digits and rounds by the absolute value. You can easily adapt this to your needs.

Regard the division by 100 using 100.0. This will insure that you always get a double as a result which is particularly important if you build a sum over the rounded values.

floor(abs(value) * 100 + 0.5)/100.0 * sign(value)

这篇关于在hibernate查询语言中舍入到2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆