C ++将数字的小数部分转换为整数 [英] c++ convert a fractional part of a number into integer
本文介绍了C ++将数字的小数部分转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将数字的小数部分转换为没有逗号的整数, 例如,我有3.35,我只想得到35个零件,没有零或逗号,
I needed to convert a fractional part of a number into integer without a comma, for example I have 3.35 I want to get just 35 part without zero or a comma,
因为我使用了modf()函数来提取小数部分,但它给出了0.35 是否有任何方法可以执行此操作或过滤"0".如果您向我展示如何使用较小的代码,我将非常感谢
Because I used the modf() function to extract the the fractional part but it gives me a 0.35 if there is any way to do that or to filter the '0.' part I will be very grateful if you show me how with the smaller code possible,
推荐答案
比转换为字符串然后再次返回要有效的多:
A bit more efficient than converting to a string and back again:
int fractional_part_as_int(double number, int number_of_decimal_places) {
double dummy;
double frac = modf(number,&dummy);
return round(frac*pow(10,number_of_decimal_places));
}
这篇关于C ++将数字的小数部分转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文