如何从C中的浮点数中提取小数部分? [英] How to extract the decimal part from a floating point number in C?

查看:492
本文介绍了如何从C中的浮点数中提取小数部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何提取浮点数的小数部分并将小数部分和整数部分存储到两个单独的整数变量中?

How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables?

推荐答案

您使用 modf 函数:

double integral;
double fractional = modf(some_double, &integral);

您也可以将其强制转换为整数,但是请注意,该整数可能会溢出。那时的结果是不可预测的。

You can also cast it to an integer, but be warned you may overflow the integer. The result is not predictable then.

这篇关于如何从C中的浮点数中提取小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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