c - 如何使小数的值仅以数字显示?

原文 标签 c floating-point decimal

我试图使我的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()(小数点)使用.标记字符串。
第一部分(令牌)是不可分割的部分,使用转换器功能进行翻译。
第二部分(令牌)是代表“分”的部分。您可以直接打印。

相关文章:

c - 气泡排序不排序和多余元素

math - 浮点数学运算是否被破坏?

c# - 十进制存储来自C#中解析字符串的精度?这意味着什么?

c - 从标准输入读取输入直到空行

c - 客户端套接字无法使用轮询/选择接收数据

matlab - (Matlab)将复杂矩阵分配给局部变量时,奇怪的精度损失

c# - 十进制C#的自定义格式

c# - 在C#中设置小数的精度

c++ - 让任务运行一定数量的指令?

c# - C#中的浮点数是否有很好的radixsort实现