如何让小数的值只显示为数字?
How to make the value of the decimal only show in numerals?
我试图让我的 c 程序 运行 你输入的任何值都会以单词的形式出现,例如,如果我想输入值 1234.56,它应该以 "One Thousand Two Hundred Thirty Four Dollars ... and 56 Cents" 的形式出现美分如何只用数字表示。到目前为止,我已经正确编写了能够将数字转换为单词的代码,但我不知道如何只留下美分部分。请帮忙!!
目前对于小数我在负数中得到了一些离谱的数字!
代码如下:
#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 [=10=].01 to 99.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");
}
为了避免int
和float
之间的混淆,避免这两种类型之间的算术运算问题,我建议
您可以使用 FLOOR 函数(四舍五入到小于实际值的最接近整数值),单独打印并从原始数字中减去它 -> 然后您将得到美分值,您可以将其乘以 100 并得到它们整数值。
您正在将 float 隐式转换为 int。赋值时 e = inclusive。这就是为什么你得到奇怪的数字,不要将整数转换为浮点数,反之亦然,永远不要!
实际上你可以使用模数得到小数部分。
也许你可以试试这个:
void printNum4(float inclusive)
{
//Decimal part * 100 to get cents
float decimal = fmod(inclusive, 1) * 100;
//Print a float with a 0 decimal precision
printf("%.0f", decimal);
}
我试图让我的 c 程序 运行 你输入的任何值都会以单词的形式出现,例如,如果我想输入值 1234.56,它应该以 "One Thousand Two Hundred Thirty Four Dollars ... and 56 Cents" 的形式出现美分如何只用数字表示。到目前为止,我已经正确编写了能够将数字转换为单词的代码,但我不知道如何只留下美分部分。请帮忙!!
目前对于小数我在负数中得到了一些离谱的数字!
代码如下:
#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 [=10=].01 to 99.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");
}
为了避免int
和float
之间的混淆,避免这两种类型之间的算术运算问题,我建议
您可以使用 FLOOR 函数(四舍五入到小于实际值的最接近整数值),单独打印并从原始数字中减去它 -> 然后您将得到美分值,您可以将其乘以 100 并得到它们整数值。
您正在将 float 隐式转换为 int。赋值时 e = inclusive。这就是为什么你得到奇怪的数字,不要将整数转换为浮点数,反之亦然,永远不要!
实际上你可以使用模数得到小数部分。
也许你可以试试这个:
void printNum4(float inclusive)
{
//Decimal part * 100 to get cents
float decimal = fmod(inclusive, 1) * 100;
//Print a float with a 0 decimal precision
printf("%.0f", decimal);
}