如何使十进制值仅显示数字? [英] How to make the value of the decimal only show in numerals?

查看:98
本文介绍了如何使十进制值仅显示数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着让我的C程序运行你把会出来作为例子的话,如果我想放在值的值1234.56应该站出来为一千二百三十四元......和56美分注意美分如何只在数字。到目前为止,我已经正确地写入了code,将能够以数字传送到的话,但我不知道怎么唯独留下一部分为美分。请帮助!

目前为小数,我得到的底片有些离谱的数字!

在code是如下:

 的#include<&stdio.h中GT;无效printNum(INT);
无效printNum2(INT);
无效printNum3(INT);
无效printNum4(INT);
诠释的main()
{    INT A = 0;
    INT B = 0;
    INT C = 0;
    INT D = 0,E = 0;    浮包容性;    的printf(欢迎来到IPC144支票发电机!\\ n);
    的printf(支付的... amahmood29 THE ORDER(018359133)\\ n);
    的printf(请输入从$ 0.01至$ 9999.99货币价值的包容性:);
    scanf函数(%F,&安培;含);    如果(含< 0.01 ||包容> 9999.99){
           的printf(对不起,不能创建支票的金额,再试一次下次的时间\\ n!);
             }
    其他
    {
        A =包容/ 1000; //此数据由潜水无论值是不是1000,100,10取代我们的变量。
        包容包容= - (A * 1000);
        B =包/ 100;
                包容包容= - (B * 100);
        如果(含> 19)
        {
                    C =包容/ 10;
            包容包容= - (C * 10);
        }
        其他
        {
            C =包容性;
            D = 0;
        }
        D =包容性;
        E =包容性; //变量E是小数//
        包容包容= - A - B - Ç        printNum(一); //打印的是变量都在几千,几百,几十或那些categories.//
        的printf(千);
        printNum(二);
        的printf(百);
        printNum2(C);
        的printf();
        printNum3(四);
        的printf(元和...);
        printNum4(E);
        的printf(仙\\ n);    }
}无效printNum(INT X)
{
    如果(X == 1)
        的printf(1);
    否则如果(X == 2)
        的printf(二);
    否则,如果(X == 3)
        的printf(三公);
    否则,如果(X == 4)
        的printf(四);
    否则,如果(X == 5)
            的printf(五);
    否则,如果(X == 6)
        的printf(六);
    否则,如果(X == 7)
        的printf(七);
    否则,如果(X == 8)
        的printf(八);
    否则,如果(X == 9)
        的printf(九);}
无效printNum2(INT X)
{
     如果(X == 10)
         的printf(十大);
     否则,如果(X == 11)
         的printf(十一);
     否则,如果(X == 12)
         的printf(十二);
     否则,如果(X == 13)
         的printf(十三);
     否则,如果(X == 14)
         的printf(十四);
     否则,如果(X == 15)
         的printf(十五);
     否则,如果(X == 16)
         的printf(十六);
     否则,如果(X == 17)
         的printf(十七);
     否则,如果(X == 18)
         的printf(十八);
     否则,如果(X == 19)
         的printf(Ninteen);
     否则如果(X == 2)
         的printf(二十);
     否则,如果(X == 3)
         的printf(三十);
     否则,如果(X == 4)
         的printf(四十);
     否则,如果(X == 5)
         的printf(五十);
     否则,如果(X == 6)
         的printf(六十条);
     否则,如果(X == 7)
         的printf(七十);
     否则,如果(X == 8)
         的printf(八十);
     否则,如果(X == 9)
         的printf(九);
}无效printNum3(INT X)
{
    如果(X == 1)
        的printf(1);
    否则如果(X == 2)
        的printf(二);
    否则,如果(X == 3)
        的printf(三公);
    否则,如果(X == 4)
        的printf(四);
    否则,如果(X == 5)
        的printf(五);
    否则,如果(X == 6)
        的printf(六);
    否则,如果(X == 7)
        的printf(七);
    否则,如果(X == 8)
        的printf(八);
    否则,如果(X == 9)
        的printf(九);}无效printNum4(INT X)
{
    的printf(%D,E);}


解决方案

要避免 INT 浮动并避免两种类型之间,arithmaic操作的问题,我建议


  • 扫描()输入为浮动 / 双击

  • 使用 的sprintf() 将其转换为字符串。

  • 标记化使用字符串 的strtok() 基础上的分隔符(小数点)。

  • 第一部分(标记)是不可或缺的一部分,使用转换器的功能来进行翻译。

  • 第二部分(标记)是部分重presenting的的。您可以直接打印出来。

Im trying to make my c program run any value that you put in will come out as words for example if I wanted to put in the value 1234.56 it should come out as "One Thousand Two Hundred Thirty Four Dollars ... and 56 Cents" Notice how the cents are only in numerals. So far I have properly written the code that will be able to transfer the numbers to words but I have no clue how to only leave the part for the cents alone. Please help!!

Currently for the decimals I get some outrageous number in the negatives!

The code is as follows :

#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");

}

解决方案

To avoid the confusion between int and float and to avoid the issues with arithmaic operations between the two types, I propose

  • scan() the input as float/double.
  • Use sprintf() to convert that as a string.
  • Tokenize the string using strtok() based on the delimiter . (decimal point).
  • First part (token) is integral part, use the converter function to translate.
  • Second part (token) is the part representing "cents". You can print it directly.

这篇关于如何使十进制值仅显示数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆