gpt4 book ai didi

c - 如何让小数点的值只显示在数字中?

转载 作者:行者123 更新时间:2023-11-30 15:19:17 25 4
gpt4 key购买 nike

我试图让我的 c 程序运行您输入的任何值都会以单词的形式出现,例如,如果我想输入值 1234.56,它应该显示为“一千二百三十四美元......并且56 美分” 请注意,美分仅以数字表示。到目前为止,我已经正确编写了能够将数字转换为单词的代码,但我不知道如何只留下美分部分。请帮忙!!

目前,对于小数,我在负数中得到了一些令人震惊的数字!

代码如下:

#include <stdio.h>

void printNum(int);
void printNum2(int);
void printNum3(int);
void printNum4(int);
int main()
{

int a = 0;
int b = 0;
int c = 0;
int d = 0, e=0;

float inclusive;

printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);

if(inclusive < 0.01 || inclusive > 9999.99) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the value is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 )
{
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
e = inclusive; //the variable "e" is for the decimal//
inclusive = inclusive - a - b - c

printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.//
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printNum4(e);
printf("Cents\n");

}
}

void printNum(int x)
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");

}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Ninteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}

void printNum3(int x)
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");

}

void printNum4(int x)
{
printf("%d, e");

}

最佳答案

为了避免 intfloat 之间的混淆,并避免这两种类型之间的算术运算问题,我建议

  • scan() 输入为 float/double
  • 使用sprintf()将其转换为字符串。
  • 使用 strtok() 对字符串进行标记基于分隔符.(小数点)。
  • 第一部分(token)是不可分割的部分,使用转换器功能进行翻译。
  • 第二部分( token )是代表“分”的部分。您可以直接打印。

关于c - 如何让小数点的值只显示在数字中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786042/

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