printf,如何为整数插入小数点 [英] printf, how to insert decimal point for integer

查看:94
本文介绍了printf,如何为整数插入小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINT16 无符号整数 say

I have an UINT16 unsigned integer of say

4455, 312, 560 or 70.

如何使用 printf 在最后两位数字前插入小数点,使示例数字显示为

How to use printf to insert a decimal point before the last two digits so the example numbers appear as

44.55, 3.12, 5.60 or 0.70

如果没有 printf 解决方案,是否还有其他解决方案?

If there is no printf solution, is there another solution for this?

我不想使用浮点数.

推荐答案

%.2d 可以添加额外的填充零

%.2d could add the extra padding zeros

printf("%d.%.2d", n / 100, n % 100);

例如,如果n560,则输出为:5.60

For example, if n is 560, the output is: 5.60

EDIT :我一开始没有注意到它是 UINT16,根据@Eric Postpischil 的评论,最好使用:

EDIT : I didn't notice it's UINT16 at first, according to @Eric Postpischil's comment, it's better to use:

printf("%d.%.2d", (int) (x/100), (int) (x%100));

这篇关于printf,如何为整数插入小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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