Number to words currency decimal

user3144640

I have this code to get Numbers to words and it works, but I have a requirement for my application and that is :

Example 1 : What the program does, let's take this number : 27,59 - The code will take me to "doua sute sapte zeci si 59 bani. Is ok, but :

Example 2 : When the number is 27.00 the result is : doua zeci si sapte si 00 bani, and the result I want only when some number has .00 to cut the 'si 00 bani' and get from 27.00 = doua zeci si sapte lei than doua zeci si sapte lei si 00 bani. Thank you

  private static string[] _ones =
        {
            "",
            "unu",
            "doua",
            "trei",
            "patru",
            "cinci",
            "sase",
            "sapte",
            "opt",
            "noua"
        };

        private static string[] _teens =
        {
            "zece",
            "unsprezece",
            "doisprezece",
            "treisprezece",
            "paisprezece",
            "cincisprezece",
            "saisprezece",
            "saptisprezece",
            "optsprezece",
            "nouasprezece"
        };

        private static string[] _tens =
        {
            "",
            "zece",
            "douazeci",
            "treizeci",
            "patruzeci",
            "cincizeci",
            "saizeci",
            "saptezeci",
            "optzeci",
            "nouazeci"
        };

        // US Nnumbering:
        private static string[] _thousands =
        {
            "",
            "mie",
            "milion",
            "miliard",
            "trilion",
            "catralion"
        };
string digits, temp;
            bool showThousands = false;
            bool allZeros = true;

            // Use StringBuilder to build result
            StringBuilder builder = new StringBuilder();
            // Convert integer portion of value to string
            digits = ((long)value).ToString();
            // Traverse characters in reverse order
            for (int i = digits.Length - 1; i >= 0; i--)
            {
                int ndigit = (int)(digits[i] - '0');
                int column = (digits.Length - (i + 1));

                // Determine if ones, tens, or hundreds column
                switch (column % 3)
                {
                    case 0:        // Ones position
                        showThousands = true;
                        if (i == 0)
                        {
                            // First digit in number (last in loop)
                            temp = String.Format("{0} ", _ones[ndigit]);
                        }
                        else if (digits[i - 1] == '1')
                        {
                            // This digit is part of "teen" value
                            temp = String.Format("{0} ", _teens[ndigit]);
                            // Skip tens position
                            i--;
                        }
                        else if (ndigit != 0)
                        {
                            // Any non-zero digit
                            temp = String.Format("{0} ", _ones[ndigit]);
                        }
                        else
                        {
                            // This digit is zero. If digit in tens and hundreds
                            // column are also zero, don't show "thousands"
                            temp = String.Empty;
                            // Test for non-zero digit in this grouping
                            if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
                                showThousands = true;
                            else
                                showThousands = false;
                        }

                        // Show "thousands" if non-zero in grouping
                        if (showThousands)
                        {
                            if (column > 0)
                            {
                                temp = String.Format("{0}{1}{2}",
                                    temp,
                                    _thousands[column / 3],
                                    allZeros ? " " : ", ");
                            }
                            // Indicate non-zero digit encountered
                            allZeros = false;
                        }
                        builder.Insert(0, temp);
                        break;

                    case 1:        // Tens column
                        if (ndigit > 0)
                        {
                            temp = String.Format("{0}{1}",
                                _tens[ndigit],
                                (digits[i + 1] != '0') ? " si " : " ");
                            builder.Insert(0, temp);
                        }
                        break;

                    case 2:        // Hundreds column
                        if (ndigit > 0)
                        {
                            temp = String.Format("{0} sute ", _ones[ndigit]);
                            builder.Insert(0, temp);
                        }
                        break;
                }
            }

            builder.AppendFormat("lei si {0:00} bani", (value - (long)value) * 100);


            // Capitalize first letter
            return String.Format("{0}{1}",
                Char.ToUpper(builder[0]),
                builder.ToString(1, builder.Length - 1));
Dmitriy Khaykin

What you need is a condition to check the decimal value. An easy way to do this is the Decimal.Remainder() method provided by .net.

Just change this

builder.AppendFormat("lei si {0:00} bani", (value - (long)value) * 100);

To include a condition for your 00...

// You always need "lei" right?
builder.AppendFormat("lei");

// This code simply divides the decimal value by 1; and only adds "si NN bani" if there's a remainder
if (Decimal.Remainder(value, 1) > 0) {
    builder.AppendFormat(" si {0:00} bani", (value - (long)value) * 100);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Method to convert Number(Currency) to Words

From Dev

Converting decimal number to currency in SQL (Oracle)

From Dev

Rails decimal (currency) number not saving when the scale part changes

From Dev

Rails number_to_currency delete trailing zeroes right of decimal

From Dev

In angular 2, how to display a number as two decimal rounded currency?

From Dev

Rails decimal (currency) number not saving when the scale part changes

From Dev

Regex to replace decimal number and all period in currency format

From Dev

Convert currency with symbol to decimal

From Dev

Format a Decimal to Currency

From Dev

Convert currency with symbol to decimal

From Dev

How to convert decimal number to words (money format) using PHP?

From Dev

Number to Words Conversion (Indian Rupee) decimal String issue in php

From Dev

Format decimal value to currency with 2 decimal places

From Dev

Money, Decimal or Numeric for Currency Columns

From Dev

Currency input with 2 decimal format

From Dev

Currency validation with Decimal using regex

From Dev

User input currency extra decimal

From Dev

Remove Decimal places from Currency?

From Dev

Currency to number conversion rails

From Dev

Validate number with currency symbol

From Dev

Format string number as currency

From Dev

Get number of currency on mysql

From Dev

Currency to number conversion rails

From Dev

Validate number with currency symbol

From Dev

How to convert currency to a number?

From Dev

Number to Words

From Dev

Formatting currency in Android using wrong decimal separator

From Dev

MVC: Problems to pass currency in decimal parameter

From Dev

Format a decimal with currency symbol and no trailing zeroes?

Related Related

HotTag

Archive