Round doubles in Objective-C

前端 未结 4 2045
长情又很酷
长情又很酷 2021-01-17 16:28

I have double number in a format like 34.123456789. How can I change it to 34.123?

I just want 3 digits after the decimal point.

4条回答
  •  难免孤独
    2021-01-17 17:06

    You can use:

    #include 
    :
    dbl = round (dbl * 1000.0) / 1000.0;
    

    Just keep in mind that floats and doubles are as close an approximation as the underlying type can provide. It may not be exact.

提交回复
热议问题