将Objective-C浮点数舍入到最接近的.05

Yas*_*qui 7 floating-point objective-c rounding

我想将以下浮点数舍入到最接近的0.05.

449.263824 - > 449.25

390.928070 - > 390.90

390.878082 - > 390.85

我怎么能做到这一点?

Bla*_*rog 13

匹配您问题中的输出,您可以执行以下操作:

float customRounding(float value) {
    const float roundingValue = 0.05;
    int mulitpler = floor(value / roundingValue);
    return mulitpler * roundingValue;
}
Run Code Online (Sandbox Code Playgroud)

例:

NSLog(@"Output: %f --> %.2f", 449.263824, customRounding(449.263824));
Run Code Online (Sandbox Code Playgroud)