DecimalFormat 小数位数
目前,我在 JTable 中显示了具有 3 位小数的 BigDecimal 值。当我得到的值为 2.500 或 1.000 时,我想要 2.5 和 1.0。这就是我尝试这个的原因:
BigDecimal value;
BigDecimal value3 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(3, BigDecimal.ROUND_HALF_UP);
BigDecimal value2 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(2, BigDecimal.ROUND_HALF_UP);
if(valor3.equals(value2))
{
BigDecimal value1 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(1, BigDecimal.ROUND_HALF_UP);
if(value3.equals(value1))
value = valuer1;
else
value = value2;
}
else
value = value3;
但它不起作用。看起来 '2.500'.equals('2.5') 是错误的。
我还尝试在其渲染器中为 JTable 提供格式:
class paramRenderer extends DefaultTableCellRenderer
{
private final DecimalFormat formatter = new DecimalFormat( "#.000" );
public void setValue() {
formatter.setMinimumFractionDigits(1);
formatter.setMaximumFractionDigits(3);
}
}
但这根本没有区别。
还有其他想法吗?
编辑:最后我在互联网上找到了一个解决方案:
BigDecimal trim(BigDecimal n)
{
try
{
while (true)
{
n = n.setScale(n.scale()-1);
}
}
catch (ArithmeticException e)
{
// Not "real" error: No more trailing zeroes -> Just exit // setScale() tries to eliminate a non-zero digit -> Out of the loop // Remember exceptions are not recommended for exiting loops, but this seems to be the best way...
}
return n;
}
并且效果很好!无论如何,非常感谢您的帮助:)
Currently, I have BigDecimal values with 3 decimal places shown in a JTable. When i get values as 2.500 or 1.000, I want to have 2.5 and 1.0 instead. That's why I tried this:
BigDecimal value;
BigDecimal value3 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(3, BigDecimal.ROUND_HALF_UP);
BigDecimal value2 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(2, BigDecimal.ROUND_HALF_UP);
if(valor3.equals(value2))
{
BigDecimal value1 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(1, BigDecimal.ROUND_HALF_UP);
if(value3.equals(value1))
value = valuer1;
else
value = value2;
}
else
value = value3;
But it doesn't work. It looks like '2.500'.equals('2.5') were false.
I also tried to give format to the JTable at its renderer:
class paramRenderer extends DefaultTableCellRenderer
{
private final DecimalFormat formatter = new DecimalFormat( "#.000" );
public void setValue() {
formatter.setMinimumFractionDigits(1);
formatter.setMaximumFractionDigits(3);
}
}
But it makes no difference at all.
Any other idea?
EDIT: Finaly I found over the Internet a solution:
BigDecimal trim(BigDecimal n)
{
try
{
while (true)
{
n = n.setScale(n.scale()-1);
}
}
catch (ArithmeticException e)
{
// Not "real" error: No more trailing zeroes -> Just exit // setScale() tries to eliminate a non-zero digit -> Out of the loop // Remember exceptions are not recommended for exiting loops, but this seems to be the best way...
}
return n;
}
And works great! Thank you very much for the help anyway :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用此格式
#.0##
作为格式化程序Try this format
#.0##
for the formatter尝试这样做,
Try to do like this,