iOS Objective-C How to get 2 decimal rounded float value?

Have you tried this?

CGFloat val = 37.777779;
CGFloat rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
CGFloat nearest = floorf(val * 100 + 0.5) / 100; /* Result: 37.78 */
CGFloat rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
source : Rounding Number to 2 Decimal Places in C
Complementing: You just don‘t have control about how the computer will store the float value.
So these rounded values may not be EXACTLY the "obvious" decimal values, but they will be very very close, and that‘s the max guarantee you will have.

You do that in Objective-C exactly the same as you would in C:
double notRounded = ...;
double rounded = round (notRounded * 100.0) / 100.0;
Don‘t use float - unless you can explain to someone exactly what your specific reasons are to use float over double. float has very limited precision. And using ceil or floor is daft, since there is a perfectly fine standard function named "round".
Neither float nor double can exactly represent most decimal values, like 12.24. Because it has much higher precision, double will let you get much closer to 12.24. Therefore, there is a huge chance that NSLog will display some weird number for (float) 12.24, like 12.23999997394 or whatever, while it may display 12.24 for double.

时间: 2024-12-15 10:04:33

iOS Objective-C How to get 2 decimal rounded float value?的相关文章

IOS 获取系统版本字符串,并且转化成float类型

pcDuino3下支持mmc启动,官方的Uboot是采用SPL框架实现的,因为内部的SRAM空间达到32K,我们完全可以在这32K空间内编写一个完整可用小巧的bootloader来完成引导Linux kernel的目的. 我们首先介绍下SPL框架,可以先看下<GNU ARM汇编--(十八)u-boot-采用nand_spl方式的启动方法>和<GNU ARM汇编--(十九)u-boot-nand-spl启动过程分析>,NAND_SPL也算是SPL框架下的一种模式. 当使用Nand f

c#中decimal ,double,float的区别

转自:http://www.cnblogs.com/lovewife/articles/2466543.html 单精度就是指4个字节的浮点数,即float双精度就是指8个字节的浮点数,即double decimal是高精度 浮点型 Name CTS Type Description Significant Figures Range (approximate) float System.Single 32-bit single-precision floating point 7 ±1.5 ×

iOS Objective -C Runtime 运行时之一: 类与对象

// --------------------------------------------------- 参考:南峰子的技术博客 http://southpeak.github.io //---------------------------------------------------- OC语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们编写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一个方法的实现等.

1不等于1?numeric、decimal、float 和 real 数据类型的区别

大家有没有在SQL中遇见1不等于1(1<>1)的情形!?下面会有一个例子演示这个情形. 先简单介绍一下标题中的四种数值数据类型. 在T-SQL中,numeric和decimal是精确数值数据类型,而float和real是近似数值数据类型. numeric和decimal是等同的.real等同于float(24). 在定义一个float(n)类型列的时候,如果明确指定了n的值,例如 val float(10) 那么当n的值在1-24之间,系统会将该列(val)标记为real类型:当n的值在25-

TSQL - Decimal, Numeric, Float &amp; Real

在SQL Server中数值类型有多种不同的分法,其中一种就是:精确类型和近似类型.其中近似类型其实只有两种数据类型:Float和Real. 在我们的数据库设计中如果设计到需要存储非整形数值的时候,很多时候我们都会疑惑到底需要用下面数据类型的哪个: Float Real Decimal Numeric 说开来其实很简单,上面4个类型其实只有两种类型: 1)     Float & Real 他们都是表示浮点数的数值类型,而且是一种近似数值表示,Real是Float的一种特殊形式. Float的语

Decimal、 Float、 Double 使用

一.Java 1.float型定义的数据末尾必须 有"f "或"F",为了和double区别.例float x=123.456f,  y=2e20f; public static void main(String[] args) { System.out.println(0.08f + 0.01f); } 输出: 0.089999996 2.精确计算中我们要用java.math.BigDecimal class DecimalUtil{ private static

decimal, double, float

refers : http://blog.leanote.com/post/weibo-007/mysql_float_double_decimal https://brooch.me/2016/11/17/%E6%B5%AE%E7%82%B9%E6%95%B0%E8%AE%A1%E7%AE%97%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E7%B2%BE%E7%A1%AE/ https://frontstuff.io/how-to-handle-monetary-

iOS开发之Objective-c的AES128加密和解密算法的实现

1 #import <Foundation/Foundation.h> 2 3 #import <CommonCrypto/CommonDigest.h> 4 5 #import <CommonCrypto/CommonCryptor.h> 6 7 #import "NSData+AES.h" 8 9 @interface NSString (AES) 10 11 //加密字符串 12 13 - (NSString*)aes128Encrypt:(N

C++ Objective C Java C 详细比较和区别

分享一下我老师大神的人工智能教程吧.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net primitive types | arithmetic and logic | strings | regexes | dates and time | arrays | dictionaries | functions execution control | files | directories | processes and