Convert number with decimals (in currency) to words

convert decimal number to words in c#
convert numeric value into words (currency) in c#
convert number to words in indian rupees
convert decimal number to words in javascript
translate convert words to numbers
converting numbers to words in visual studio
convert number to words in c# windows application
c# number to words library

I want to convert numbers With decimal (in currency )to word

ex.: 12345.60 --> twelve thousand three hundred forty-five dollars and sixty cents

i got this code from http://www.csharp-tutorials.info/2016/04/convert-numbers-to-words-in-c.html

 public static string NumberToWords(int number)
    {
        if (number == 0)
            return "zero";

        if (number < 0)
            return "minus " + NumberToWords(Math.Abs(number));

        string words = "";

        if ((number / 1000000000) > 0)
        {
            words += NumberToWords(number / 1000000000) + " billion ";
            number %= 1000000000;
        }

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " million ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " thousand ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " hundred ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += " ";

            var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
            var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }

        return words;
    }

it work totally fine with integers but if input double.. it shows error

because it only accept int.

I try my best with my knowledge but I cant alter the code to get what i want..

The problem is that you use modulo on doubles, which is (obviously) not allowed. you have to use Math.Floor(number) with the given code for the part before the floating point and number - Math.Floor(number) for the part after the floating point. The rest is actually given in your code example, just add "Dollar" after the part before the floating point and "cents" after the part after the floating point. Your code would look somwhat like that:

    public static string NumberToWords(double doubleNumber)
    {
        var beforeFloatingPoint = (int) Math.Floor(doubleNumber);
        var beforeFloatingPointWord = $"{NumberToWords(beforeFloatingPoint)} dollars";
        var afterFloatingPointWord =
            $"{SmallNumberToWord((int) ((doubleNumber - beforeFloatingPoint) * 100), "")} cents";
        return $"{beforeFloatingPointWord} and {afterFloatingPointWord}";
    }

    private static string NumberToWords(int number)
    {
        if (number == 0)
            return "zero";

        if (number < 0)
            return "minus " + NumberToWords(Math.Abs(number));

        var words = "";

        if (number / 1000000000 > 0)
        {
            words += NumberToWords(number / 1000000000) + " billion ";
            number %= 1000000000;
        }

        if (number / 1000000 > 0)
        {
            words += NumberToWords(number / 1000000) + " million ";
            number %= 1000000;
        }

        if (number / 1000 > 0)
        {
            words += NumberToWords(number / 1000) + " thousand ";
            number %= 1000;
        }

        if (number / 100 > 0)
        {
            words += NumberToWords(number / 100) + " hundred ";
            number %= 100;
        }

        words = SmallNumberToWord(number, words);

        return words;
    }

    private static string SmallNumberToWord(int number, string words)
    {
        if (number <= 0) return words;
        if (words != "")
            words += " ";

        var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

        if (number < 20)
            words += unitsMap[number];
        else
        {
            words += tensMap[number / 10];
            if ((number % 10) > 0)
                words += "-" + unitsMap[number % 10];
        }
        return words;
    }

Convert number with decimals (in currency) to words, The problem is that you use modulo on doubles, which is (obviously) not allowed. you have to use Math.Floor(number) with the given code for the part before the� I want to convert numbers With decimal (in currency )to word. ex.: 12345.60 --> twelve thousand three hundred forty-five dollars and sixty cents.

As an addon to the above answer, since most of the systems are globalised these days it is important to support multiple currencies and support conversion according to the different way a country recognises decimal groups. I solved it by creating template in a json file for each key currency i wanted to support and then use the code similar to above (without the hardcoding) to read data from template and convert accordingly. eg of template is below, let me know if anyone needs the code.

Json template for USD to words

    {
    "currencyKey": "USD",
    "formatMainUnit": "{0} dollar",
    "formatDecimalUnit": "{0} cent",
    "joinMainAndDecimal": "and",
    "ifNoDecimalUnit": "",
    "formatForMinus": "minus {0}",
    "unitsMap": [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
    "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"],
    "tensMap": [ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ],
    "groupMap": [
        {"tenRaiseTo":9, "word":"billion"},
        {"tenRaiseTo":6, "word":"million"},
        {"tenRaiseTo":3, "word":"thousand"},
        {"tenRaiseTo":2, "word":"hundred"}
    ]
}

Json template for INR to words

{
    "currencyKey": "INR",
    "formatMainUnit": "{0} rupee",
    "formatDecimalUnit": "{0} paisa",
    "joinMainAndDecimal": "and",
    "ifNoDecimalUnit": "zero paisa",
    "formatForMinus": "minus {0}",
    "unitsMap": [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
    "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"],
    "tensMap": [ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ],
    "groupMap": [
        {"tenRaiseTo":7, "word":"crore"},
        {"tenRaiseTo":5, "word":"lak"},
        {"tenRaiseTo":3, "word":"thousand"},
        {"tenRaiseTo":2, "word":"hundred"}
    ]
}

Json template for Thai Baht to words

{
    "currencyKey": "THB",
    "formatMainUnit": "{0} baht",
    "formatDecimalUnit": "{0} satang",
    "joinMainAndDecimal": "and",
    "ifNoDecimalUnit": "No satang",
    "formatForMinus": "minus {0}",
    "unitsMap": [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
    "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"],
    "tensMap": [ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ],
    "groupMap": [
        {"tenRaiseTo":9, "word":"billion"},
        {"tenRaiseTo":6, "word":"million"},
        {"tenRaiseTo":3, "word":"thousand"},
        {"tenRaiseTo":2, "word":"hundred"}
    ]
}

Numbers to Words Converter, Convert a number to USD currency and check writing amounts rounded to 2 decimal places. Choose to have words for the numbers in lowercase, uppercase or� How to convert amounts of money, without decimals, to words in US American English? To write an integer number we first must know the place value of each digit. For example, the number 1,234,567 has a 1 in the millions place, a 2 in the hundred thousands, a 3 in the ten thousands place, a 4 in the thousands place, a 5 in the hundreds place, a 6

Your mapping arrays helped inspire this result, but I went a different route to handle the places. Works well for me. It is geared for American currency. There are a couple of extension methods used (viz. RemoveDoubleSpaces(), TryIntParse()) that will need to be accounted for, but what they do is pretty obvious.

    public static string ToVerbalCurrency(this double value)
    {
        var valueString = value.ToString("N2");
        var decimalString = valueString.Substring(valueString.LastIndexOf('.') + 1);
        var wholeString = valueString.Substring(0, valueString.LastIndexOf('.'));

        var valueArray = wholeString.Split(',');

        var unitsMap = new[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        var tensMap = new[] { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
        var placeMap = new[] { "", " thousand ", " million ", " billion ", " trillion " };

        var outList = new List<string>();

        var placeIndex = 0;

        for (int i = valueArray.Length - 1; i >= 0; i--)
        {
            var intValue = valueArray[i].TryIntParse();
            var tensValue = intValue % 100;

            var tensString = string.Empty;
            if (tensValue < unitsMap.Length) tensString = unitsMap[tensValue];
            else tensString = tensMap[(tensValue - tensValue % 10) / 10] + " " + unitsMap[tensValue % 10];

            var fullValue = string.Empty;
            if (intValue >= 100) fullValue = unitsMap[(intValue - intValue % 100) / 100] + " hundred " + tensString + placeMap[placeIndex++];
            else if (intValue != 0) fullValue = tensString + placeMap[placeIndex++];
            else placeIndex++;

            outList.Add(fullValue);
        }

        var intCentsValue = decimalString.TryIntParse();

        var centsString = string.Empty;
        if (intCentsValue < unitsMap.Length) centsString = unitsMap[intCentsValue];
        else centsString = tensMap[(intCentsValue - intCentsValue % 10) / 10] + " " + unitsMap[intCentsValue % 10];

        if (intCentsValue == 0) centsString = "zero";

        var output = string.Empty;
        for (int i = outList.Count - 1; i >= 0; i--) output += outList[i];
        output += " dollars and " + centsString + " cents";

        return output.RemoveDoubleSpaces();
    }

Convert numbers into words (from 0 to 999,999,999,999,999 , Convert Dollars and Cents, Euro and Cent, numbers (cardinal or ordinal integers) , Mixed Decimal or decimal fractions into words with free online calculators. Decimal to words conversion calculator that represents the decimal number in English words. The representation help parents to assist their kids studying 4th, 5th or 6th grade to verify the answers of decimal to words homework and assignment problems in pre-algebra or in number system (NS) of common core state standards (CCSS) for mathematics.

C# Number (Decimal) to word converter in Dollar and Cents. � GitHub, string strWords = GetWords(input) + " Dollars";. if (decimals.Length > 0). {. // if there is any decimal part convert it to words and add it to strWords. Convert Currency To Words in Lakh, Crores, etc. Convert Amount in Words and Separate Amount with comma in Bangladeshi Currency Format using C#.NET XSLT Number to String Transformation II Convert Numeric Currency into Words for International Currency - Part - II (Optimized) Converting numbers into English

Numbers to Words Converter, You can use this online converter to convert any numbers or currencies into words. To use our converter just fill in any number and then select the currency from� Introduction. In some situations, we will get a requirement like converting a number into words. Example. 568 = Five Hundred Sixty Eight only. Here, I will explain how to convert this numeric value to words (In Indian Currency format), using C#, not only whole numbers but also with decimal and negative values.

Best Online tool to convert Numbers to Words and Share it to your , Number Utilities. All NumbersConverter � Decimal toBinary Converter � Decimal to Octal Converter � Binary to Decimal Converter � Binary to Hex Converter� Enter the whole number in the box to the left of the decimal point. Enter the decimal number in the box to the right of the decimal point. Type the number in the box and then click "Click to Convert" If the number is 346,894 then type "346,894" (no quotation marks). Do not type a decimal point. If the number is 2154 you may type "2,154", you may type the comma. Type "2,154".

Comments
  • How about taking the Math.Floor() value, subtract that from the decimal (store this in a variable, e.g. xx) and run the whole number through the function you already have. Then append xx cents (or whatever fractional currency unit) to the end.