如何将一笔钱转换为印度货币格式(印度数字格式)?

时间:2017-05-05 10:26:44

标签: c

我想构建一个C程序,将任何数字转换为印度货币格式。它可以带有负号,前导零和小数点+尾数的输入,它将格式化输入以处理前导零并根据印度数字foramt添加逗号。例如,印度数字格式的数字表示为:

1
10 
100 
1,000 
10,000 
1,00,000 
10,00,000 
1,00,00,000 
10,00,00,000

输入和相关输出如下:

Minus sign: "-12345" -> "-12,345"
Decimal Point: "-12345.123" -> "-12,345.123"
Leading Zero's: 000000.123 → "0.123" or "-000123456.1234" -> "-1,23,456.1234"

如果有人想在时间和空间复杂性方面优化此代码,请将其设置得更简洁,然后分享您的代码。

2 个答案:

答案 0 :(得分:2)

印度数字用十进制表示,数字组用逗号分隔。最后一组有3位数字,所有更高位数都成对分组。

这是一个简单的解决方案:

function loopThis(input){
    for(var i in data){
        data[i].color = "black";
    }
    var tmp = Math.floor(input/50);
    data[tmp].color = "red";
}

答案 1 :(得分:0)

添加逗号的基本逻辑是首先将符号字符(如果存在)复制到第二个数组,然后将其从原始数字数组中删除。然后,如果Exponent部分中的位数是奇数,则将1位数复制到第二个数组并移除以使长度均匀。然后对于索引的每个奇数值' i'添加逗号。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *convertToInrFormat(char *lp_number, int lv_l)
{
    char *lp_fnumber = '\0';
    char *lp_p = '\0';
    int lv_index = 0;
    int lv_nsize = 0;
    int lv_zerocount = 0;
    int sign_count = 0;
    int expo_count = 0;
    int lv_i = 0;
    int lv_j = 0;

    if (lp_number[0] == '-') // The 0th position of the char array is checked for negative sign, if found sign_count is incremented.
    {
        sign_count++;
    }

    lv_zerocount = strspn(lp_number + sign_count, "0"); // the no. of Leading Zeros is calculated ignoring the negative sign if present.

    if (lp_number[sign_count + lv_zerocount] == '.') //if the exponent part consists of only 0's then the zerocount is reduced by 1 to leave behind 1 zero in the exponent part.
    {
        lv_zerocount = lv_zerocount - 1;
    }

    if (lv_zerocount > 0) //the zeros are removed by being overwritten
    {
        memmove(lp_number + sign_count, lp_number + lv_zerocount + sign_count, strlen(lp_number));
    }

    while (lp_number[sign_count] != '.' && lp_number[sign_count] != '\0') //the count of remaining exponents is taken
    {
        expo_count++;
        sign_count++;
    }

    lv_l = strlen(lp_number); // New string length

    if (expo_count > 3) //inserting the commas
    {
        lv_nsize = lv_l + (expo_count / 2 - 1) + 1;
        lp_fnumber = (char *)malloc(lv_nsize);

        if (lp_fnumber != NULL)
        {
            if (lp_number[0] == '-')
            {
                lp_fnumber[0] = lp_number[0];
                lv_j++;
                memmove(lp_number, lp_number + 1, strlen(lp_number));
                lv_l--;

                if (expo_count % 2 != 0)
                {
                    lp_fnumber[1] = lp_number[0];
                    lv_j++;
                    memmove(lp_number, lp_number + 1, strlen(lp_number));
                    expo_count--;
                }
            }
            else if (expo_count % 2 != 0)
            {
                lp_fnumber[0] = lp_number[0];
                lv_j = lv_j + 1;
                memmove(lp_number, lp_number + 1, strlen(lp_number));
                expo_count--;
            }

            lp_p = strchr(lp_number, '.');
            if (lp_p != NULL)
            {
                lv_index = lp_p - lp_number;
            }

            while (lv_i < expo_count)
            {
                lp_fnumber[lv_j++] = lp_number[lv_i++];

                if (lv_i + 2 < expo_count && lv_i % 2 != 0) //Alt logic:((lv_i ^ lv_l) & 1) here for every odd value of i index a comma is added.
                    lp_fnumber[lv_j++] = ',';
            }
            if (lv_index != 0)
            {
                while (lp_number[lv_index] != '\0')
                {
                    lp_fnumber[lv_j++] = lp_number[lv_index++];
                }
            }
            lp_fnumber[lv_j] = '\0';
        }
        return lp_fnumber;
    }
    else
    {
        return lp_number;
    }
}

   int main()
{
    char lp_number[255];
    int lv_l;
    char *formated_number;
    printf("Enter the lp_number\n");
    fgets(lp_number, 255, stdin);
    lv_l = strlen(lp_number);
    formated_number = convertToInrFormat(lp_number, lv_l);
    puts(formated_number);
}