本文介绍了将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>
$ 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 00:43