xcode 从浮点数中获取小数部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10057788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:54:17  来源:igfitidea点击:

Get the Decimal part out of a float number

objective-cxcodefloating-point

提问by newton_guima

I need to get the decimal part out of a float number, for example:

我需要从浮点数中取出小数部分,例如:

float x = 18.30;

浮动 x = 18.30;

I need a way to get the '.30' in another float.. so I will have a float equals to 18.30 and another one equals to .30

我需要一种方法来在另一个浮点数中获取 '.30' .. 所以我将有一个等于 18.30 的浮点数和另一个等于 0.30 的浮点数

Any ideas?

有任何想法吗?

回答by Thiem Nguyen

I'm not sure if there is a function doing exactly the way you want but you can use this:

我不确定是否有一个函数完全按照你想要的方式工作,但你可以使用这个:

x - floor(x)

回答by JKvr

The accepted answer above (x - floor(x)) works for positive numbers but does not the right thing for negative numbers. Use trunc() instead:

上面接受的答案 (x - floor(x)) 适用于正数,但不适用于负数。使用 trunc() 代替:

x - trunc(x)

But the fmod() solution is the one to use if you ask me:

但是如果你问我, fmod() 解决方案是可以使用的:

fmod(x, 1)

回答by Ken Thomases

The modf()function does this more directly than fmodf().

modf()函数比fmodf().

回答by gbulmer

There are a family of fmod functions, e.g. fmod(18.30, 1.0), fmodf(18.30, 1.0), etc

有一系列 fmod 函数,例如 fmod(18.30, 1.0)、fmodf(18.30, 1.0) 等

#include <stdio.h>
#include <math.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    printf("%f\n", fmod(18.30, 1.0));
    return 0;
}

回答by aka.nice

Note: use modf or fmod as suggested in other answers.

注意:按照其他答案中的建议使用 modf 或 fmod。

But don't expect to have exactly the same number as atof("0.30") - as your equals request suggests.

但是不要期望与 atof("0.30") 的数字完全相同 - 正如您的 equals 请求所暗示的那样。

Indeed, it happens that atof("18.30") is the nearest float to atof("18.0") + atof("0.30"), but this operation is not exact, it has a rounding error, as would any sum of atof("0.30") with an integer > 0.

事实上,atof("18.30") 恰好是最接近 atof("18.0") + atof("0.30") 的浮点数,但这个操作并不准确,它有一个舍入误差,任何 atof( "0.30") 的整数 > 0。

So when you subtract back the integer part, there is no reason to find atof("0.30") back, you only find something near atof("0.30").

所以当你减去整数部分时,没有理由找回 atof("0.30"),你只能找到 atof("0.30") 附近的东西。

Expressed in Squeak/Pharo Smalltalk (double precision used here)

用 Squeak/Pharo Smalltalk 表示(此处使用双精度)

(1 to: 100) count: [:i | 0.3 + i = (3/10+i) asFloat].

gives

-> 100

But:

但:

(1 to: 100) count: [:i | (3/10+i) asFloat - i = 0.3].

gives

-> 0

回答by Lokesh G

x=3.5

x=3.5

y=parseInt(3.5)

y=parseInt(3.5)

fraction_part =( x-y );

分数_部分 =( xy );

回答by Abdurrahman Mubeen Ali

The best way I found using the Objective-C variable types is as following:

我发现使用 Objective-C 变量类型的最佳方法如下:

CGFloat floatingPointNumber = 18.30;
NSInteger integerNumber = floatingPointNumber;
CGFloat floatingPointNumberWithoutTheInteger = floatingPointNumber - integerNumber;

Following are the result when executed:

以下是执行时的结果:

enter image description here

在此处输入图片说明