【问题标题】:How to make the value of the decimal only show in numerals?如何使小数点的值仅以数字显示?
【发布时间】:2015-06-11 16:10:40
【问题描述】:

我试图让我的 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");

}

【问题讨论】:

  • 或者把数字当成字符串,在“.”的位置拆分

标签: c floating-point decimal


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以使用 FLOOR 函数(四舍五入到小于实际值的最接近的整数值),单独打印并从原始数字中减去它 -> 然后您将获得美分值,您可以将其乘以 100 并获得它们的整数值.

    【讨论】:

      【解决方案3】:

      您正在将浮点数隐式转换为 int。当分配 e = 包括在内时。这就是为什么你会得到奇怪的数字,不要将整数转换为浮点数,反之亦然,永远不要!

      其实你可以通过取模得到小数部分。

      也许你可以试试这个:

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-23
        • 2012-05-22
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-02-12
        • 1970-01-01
        • 2012-06-30
        相关资源
        最近更新 更多