首页 \ 问答 \ 如何将BigDecimal舍入小于1(How to round BigDecimal smaller than 1)

如何将BigDecimal舍入小于1(How to round BigDecimal smaller than 1)

我想将十进制数舍入到最接近的自然数。 例:

public static void main(String[] arguments){
    BigDecimal a=new BigDecimal("2.5");
    BigDecimal b=new BigDecimal("0.5");
    System.out.println(a.round(new MathContext(1,RoundingMode.UP)));
    System.out.println(b.round(new MathContext(1,RoundingMode.UP)));       
    }

预期产出

3
1

实际输出

3
0.5

问题是数字0.5舍入到0.5而不是1如何舍入小于1的BigDecimal


I want to round decimal number to nearest Natural Number. Example:

public static void main(String[] arguments){
    BigDecimal a=new BigDecimal("2.5");
    BigDecimal b=new BigDecimal("0.5");
    System.out.println(a.round(new MathContext(1,RoundingMode.UP)));
    System.out.println(b.round(new MathContext(1,RoundingMode.UP)));       
    }

Expected output

3
1

Real output

3
0.5

The problem is that number 0.5 is rounded to 0.5 instead of 1 How to Round BigDecimal smaller than 1


原文:https://stackoverflow.com/questions/31918364
更新时间:2023-08-04 06:08

最满意答案

jsut改变这个方法如下

 func actionItemNameTap(_ sender: UITapGestureRecognizer) {
  //  let view = sender.view;
   // print("\(view?.tag)")
    print("item tap")
    let indexPath = IndexPath(row: (sender.view?.tag)!, section: 0)
    let cell = tableView.cellForRow(at:indexPath )
    self.showPopOverBox(cell: cell!)  
 }

jsut change this method like below

 func actionItemNameTap(_ sender: UITapGestureRecognizer) {
  //  let view = sender.view;
   // print("\(view?.tag)")
    print("item tap")
    let indexPath = IndexPath(row: (sender.view?.tag)!, section: 0)
    let cell = tableView.cellForRow(at:indexPath )
    self.showPopOverBox(cell: cell!)  
 }

相关问答

更多
  • 只需将UITextField添加到像这样的UITableViewCell 。 UITextField* textField = [[UITextField alloc] initWithFrame:frame]; [cell addSubview:textField]; just add the UITextField to a UITableViewCell like this.. UITextField* textField = [[UITextField alloc] initWithFrame ...
  • 对此问题的简单/快速解决方法是使用充当标志的全局变量 最初标志应为0 / false 在事件触发时开始滚动检查标志是否上升(wasClosed) 如果没有,那么显示弹出,否则保持隐藏。 一旦用户点击关闭按钮,该标志将切换为true。