简体   繁体   English

如何从浮点数中得到精确的小数部分作为整数?

[英]How to get the exact fractional part from a floating point number as an integer?

There is a lot of questions and answer about decimal & integer extraction from floating point numbers and taking output for some specific decimal points. 关于浮点数的十进制和整数提取以及某些特定小数点的输出,有很多问题和答案。 But none could solve my problem. 但没有人能解决我的问题。 Please if anyone can help me to solve my problem- 如果有人能帮助我解决我的问题,请

I was actually trying to extract the exact fractional part from a floating point number. 我实际上是试图从浮点数中提取精确的小数部分。 I tried with this: 我试过这个:

float f=254.73;

int integer = (int)f;
float fractional = f-integer;

printf ("The fractional part is: %f", fractional);

But the output is: 0.729996. 但输出为:0.729996。 For this reason when I was doing this: 出于这个原因,当我这样做时:

float f=254.73;

int integer = (int)f;
float fractional = f-integer;
int fractional_part_in_integer = ((int)(f*100)%100);

printf ("The value is: %d", fractional_part_in_integer);

It gives me 72 as output. 它给了我72作为输出。 But, I want to extract exactly 73 from the given number 254.73. 但是,我想从给定的数字254.73中精确地提取73。 I already know how to use %.2f during printf() function to print upto two decimal numbers. 我已经知道如何在printf()函数中使用%.2f来打印最多两个十进制数字。 But in my code I don't want to print the number right now. 但在我的代码中,我现在不想打印这个数字。 I have some calculations with that fractional part as integer form ie 73. So, my problem is how could I extract the fractional part from 254.73 so that I can get exact 73 as integer to do more calculations? 我有一些计算,其中小数部分为整数形式,即73.所以,我的问题是如何从254.73中提取小数部分,以便我可以得到精确的73作为整数来进行更多的计算?

How to get the exact fractional part from a floating point number as an integer? 如何从浮点数中得到精确的小数部分作为整数?

trying to extract the exact fractional part from a floating point number. 试图从浮点数中提取精确的小数部分。

Use modf() or modff() 使用modf()modff()

double modf(double value, double *iptr);
float modff(float value, float *iptr);

The modf functions break the argument value into integral and fractional parts, ... modf函数将参数值分解为整数和小数部分,......
C11 §7.12.6.12 2 C11§7.12.6.122

#include <math.h>

double value = 1.234;
double ipart;
double frac = modf(value, &ipart);

A better approach for OP's need may be to first round a scaled value and then back into whole and fractional parts. 对OP的需求更好的方法可能是首先舍入一个缩放值,然后再回到整个和小数部分。

double value = 254.73;
value = round(value*100.0);

double frac = fmod(value, 100);  // fmod computes the floating-point remainder of x/y.
double ipart = (value - frac)/100.0;

printf("%f %f\n", ipart, frac);
254.000000 73.000000

Ref detail: When OP uses 254.73 , this is converted to the nearest float value which may be 254.729995727539... . 参考细节:当OP使用254.73 ,它被转换为最近的float值,可能是254.729995727539...

float f = 254.73;
printf("%.30f\n", f);
// 254.729995727539062500000000000000

You can use sprintf and sscanf to print the value to a string and then extract the fraction. 您可以使用sprintf和sscanf将值打印到字符串,然后提取分数。 The %*d scans and discards the first integer of the formatted string. %*d扫描并丢弃格式化字符串的第一个整数。 A dot is scanned and then the fraction. 扫描点然后扫描分数。

#include <stdio.h>        

int main( void)           
{                         
    char fp[30];          
    int fraction;         
    float f = 254.73f;    

    sprintf ( fp, "%.2f", f);
    sscanf ( fp, "%*d.%d", &fraction);
    printf ( "%d\n", fraction);

    return 0;                  
}

The easiest way is to use standard library function ceil from <math.h> . 最简单的方法是使用<math.h>标准库函数ceil
The float number 254.73 may be converted to 254.7299957275390625000000 . float 254.73可能会转换为254.7299957275390625000000
f-integer will give 0.7299957275390625000000 . f-integer将给出0.7299957275390625000000
Now multiply it by 100 and use ceil function to get the smallest integer value not less than 72.99957275390625000000 . 现在将它乘以100并使用ceil函数获得不小于72.99957275390625000000的最小整数值。

int fractional_part_in_integer = ((int)ceil(fractional*100)) % 100;

UPDATE: As pointed in a comment by @Sneftel , the above suggested method in this answer will not work consistently. 更新:正如@Sneftel评论所指出的,上述建议的方法在这个答案中不会一致。

A simple hack is to use round function from math.h to round the f and then extract the fractional part 一个简单的hack是使用math.h round函数来舍入f然后提取小数部分

float f=254.73;

int int_part = (int)f;
float fractional = round(f*100)/100 - int_part;
int fractional_part_in_integer = (int)(fractional*100);

printf("%d, %d\n ", int_part, fractional_part_in_integer);

Output: 输出:

254, 73

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将浮点数分解为其整数部分和小数部分 - Decompose a floating-point number into its integral and fractional part 我应该如何获得浮点值的小数部分? - How should I obtain the fractional part of a floating-point value? 如何使用 c 将双精度的小数部分转换为字符串或双精度的小数部分到 integer 而不更改确切值 - How to convert fractional part of double to string or fractional part of double to integer using c without changing exact value 如何在Arduino或C中将十进制数的整数和小数部分作为两个整数来获取? - How to get the integer and fractional part of a decimal number as two integers in Arduino or C? 如何从C中的浮点数中提取小数部分? - How to extract the decimal part from a floating point number in C? 如何修复将 integer 与小数部分分开的代码 - How to fix code for separating integer from fractional part 如何打印浮点数的 EXACT 值? - How do you print the EXACT value of a floating point number? 如何将浮点数分成整数和小数部分? - How to separate float into an integer and a fractional part? 使用C语言存储.dat文件中的整数和浮点数 - Store a integer and floating point number from a .dat file in C language 如何检查一个巨大的浮点数是否为整数? - How to check whether a huge floating point number is an integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM