由小数位的双楼 [英] floor double by decimal place

查看:118
本文介绍了由小数位的双楼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想地板双其具有可变长度的十进制(在iPhone SDK)的小数位。

下面一些例子向您展示我的意思

 的NSLog(@%F,[自主floorMyNumber:34.52462 toPlace:2); //应该返回34.52
的NSLog(@%F,[自主floorMyNumber:34.52662 toPlace:2); //应该返回34.52的NSLog(@%F,[自主floorMyNumber:34.52432 toPlace:3); //应该返回34.524
的NSLog(@%F,[自主floorMyNumber:34.52462 toPlace:3); //应该返回34.524的NSLog(@%F,[自主floorMyNumber:34.12462 toPlace:0); //应该返回34.0
的NSLog(@%F,[自主floorMyNumber:34.92462 toPlace:0); //应该返回34.0

任何想法如何做到这一点?

解决方案

   - (双)floorNumberByDecimalPlace:(浮点)数位:(INT){到位
    回报(双)((unsigned int类型)(*号(双)POW(10.0,(双)的地方)))/(双)POW(10.0,(双)的地方);
}


解决方案

另一种解决方案:

放置

10(实施例:13.1),100(实施例:12.31)等

双值=(双)((unsigned int类型)(*值(双)放置))/(双)放在

i want to floor a double by its decimal place with variable decimal length (in iphone sdk).

here some examples to show you what i mean

NSLog(@"%f",[self floorMyNumber:34.52462 toPlace:2); // should return 34.52
NSLog(@"%f",[self floorMyNumber:34.52662 toPlace:2); // should return 34.52

NSLog(@"%f",[self floorMyNumber:34.52432 toPlace:3); // should return 34.524
NSLog(@"%f",[self floorMyNumber:34.52462 toPlace:3); // should return 34.524

NSLog(@"%f",[self floorMyNumber:34.12462 toPlace:0); // should return 34.0
NSLog(@"%f",[self floorMyNumber:34.92462 toPlace:0); // should return 34.0

any ideas how to do this?

solution

-(double)floorNumberByDecimalPlace:(float)number place:(int)place {
    return (double)((unsigned int)(number * (double)pow(10.0,(double)place))) / (double)pow(10.0,(double)place);
}

解决方案

Another solution:

placed is 10 (Example: 13.1), 100 (Example: 12.31) and so on

double value = (double)((unsigned int)(value * (double)placed)) / (double)placed

这篇关于由小数位的双楼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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