如何将双数的两个部分分开并将其转换为C中的int? [英] How do I separate two parts of a double number and turn it into an int in C?

查看:86
本文介绍了如何将双数的两个部分分开并将其转换为C中的int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我得到了:



18.75但是我想将它们分成两个不同的变量,18和75?



我尝试了什么:



我尝试过modf函数并转换为int,但是除此之外还有更简单的方法吗?

For example, I got:

18.75 but I want to separate them into two different variables, 18 and 75?

What I have tried:

I've tried the modf function and converted to an int, but is there an easier way to do it other than this?

推荐答案

引用:

但除此之外还有更简单的方法吗?

but is there an easier way to do it other than this?

不,据我所知。提取积分部分很简单:只需将double赋值给整数变量即可。但是,将小数部分转换为整数不太明显。毕竟, modf 功能应该存在。

No, As far as I know. Extracting the integral part is simple: just assign the double to an integer variable. However, transforming the decimal part to an integer is less obvious. After all, there should be a reason the modf function exists.


看一下modf函数

C语言:modf函数(拆分成整数和小数部分) [ ^ ]



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]



[更新]

Have a look at the modf function
C Language: modf function (Split into Integer and Fractional Parts)[^]

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]

[Update]
引用:

我尝试过modf函数并转换为int,但除此之外还有更简单的方法吗?

I've tried the modf function and converted to an int, but is there an easier way to do it other than this?

modf有什么问题?它完全符合你的要求。

What is your problem with modf ? it does exactly what you want.


这篇关于如何将双数的两个部分分开并将其转换为C中的int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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