Limiting double to 3 decimal places

前端 未结 8 2199
小鲜肉
小鲜肉 2020-11-27 04:38

This i what I am trying to achieve:

If a double has more than 3 decimal places, I want to truncate any decimal places beyond the third. (do not round.)



        
8条回答
  •  北海茫月
    2020-11-27 04:44

    You can use:

    double example = 12.34567;
    double output = ( (double) ( (int) (example * 1000.0) ) ) / 1000.0 ;
    

提交回复
热议问题