将 float 转换为 int
我有这样的作业,您必须从用户那里获取代表美元值的 float 类型的输入 例如
4.05,
所以我想将此浮点数转换为int,使其以分为单位,因此int的值应该是405,我需要在C中执行此操作,任何人都可以提供一些帮助吗?
I'm have this assignment where you have to get an input of type float from the user which represents a value in dollars
for example
4.05
so I want to convert this float to an int such that it becomes in cents so the value of the int should be 405 , I need to do this in C, anyone can provide some help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于这是家庭作业,因此这里有一些引导性问题:
Since this is homework, here are some leading questions:
谷歌上的第一个结果是“float to int c”:http://www.cs。 tut.fi/~jkorpela/round.html。下次请谷歌一下!
First result on google with "float to int c": http://www.cs.tut.fi/~jkorpela/round.html. Next time, please google it !
这对于您的问题来说是偶然的,但如果您决定进一步发展您的编程生涯并想要编写涉及非整数金钱值的代码,那么使用某种 十进制浮点库以避免精度错误。
这是针对 Python 的,但解释了使用正确的十进制数字与货币的基本原理:
http://docs.python .org/library/decimal.html
在这个作业中,如果没有它,你可能会没事,但使用它和/或在课堂上提出它可能会为你的老师赢得一些额外的学分。 ;)
This is sort of incidental to your question but in case you should decide to go further with your programming career and want to code something that involves non-integer values of money, you're safest using some kind of decimal floating point library to avoid precision errors.
This is for Python but explains the rationale for using proper decimal numbers with currencies:
http://docs.python.org/library/decimal.html
In this assignment you'll probably be fine without it but using it and/or bringing it up in class might win a few extra credit points with your teacher. ;)