Round doubles in Objective-C

前端 未结 4 2046
长情又很酷
长情又很酷 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条回答
  •  萌比男神i
    2021-01-17 17:24

    The approved solution has a small typo. It's missing the "%".

    Here is the solution without the typo and with a little extra code.

    double d = 1.23456;
    NSString* myString = [NSString stringWithFormat:@"%.3f",d];
    

    myString will be "1.234".

提交回复
热议问题