将Objective-C浮点四舍五入到最接近的.05 [英] Rounding an Objective-C float to the nearest .05

查看:201
本文介绍了将Objective-C浮点四舍五入到最接近的.05的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把下面的浮点数凑到最接近的0.05。

449.263824 - > 449.25

390.928070 - > 390.90

390.878082 - > 390.85

我该如何做到这一点?

解决方案<在你的问题中匹配输出,你可以执行以下操作:

$ p $ float $ customRounding(float value) {
const float roundingValue = 0.05;
int mulitpler = floor(value / roundingValue);
return mulitpler * roundingValue;

$ / code


$例子:

<$ (@输出:%f - >%.2f,449.263824,customRounding(449.263824)); p $ p> NSLog


I want to round the following floating point numbers to the nearest 0.05.

449.263824 --> 449.25

390.928070 --> 390.90

390.878082 --> 390.85

How can I accomplish that?

解决方案

The match the output in your question, you can do the following:

float customRounding(float value) {
    const float roundingValue = 0.05;
    int mulitpler = floor(value / roundingValue);
    return mulitpler * roundingValue;
}

Example:

NSLog(@"Output: %f --> %.2f", 449.263824, customRounding(449.263824));

这篇关于将Objective-C浮点四舍五入到最接近的.05的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆