gpt4 book ai didi

c - 如何取任何整数,例如 10050,并将其输出为 $100.50?

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:09 24 4
gpt4 key购买 nike

例子:

int amount = 10050;
printf("format_string", amount);
// What should the format string look like to get $100.50 as the output?

是否可以告诉 printf 函数我想在右边的两位数字上放置一个点,而无需执行以下操作:

int amount = 10050;
printf("$%d.%02d", amount / 100, amount % 100); // Output: $100.50

最佳答案

您可以将其转换为 float,除以 100,然后以浮点格式打印。

printf("$%.2f", ((double)amount)/100);

关于c - 如何取任何整数,例如 10050,并将其输出为 $100.50?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com