Convert Numbers To Words In asp.net C#
Microsoft Net Framework

Convert Numbers To Words In asp.net C#

 

Problem:

sometimes, we display amount on website like this Price: 14,750000 RS, it is very difficult to read,

so we write it in words , its very meaningful like  Fourteen Lac and fifty thounds.....

below is some code that can convert amount from number to string.


Solutions:


public class ConvertAmountToString
{  
    private static String[] units = { "Zero", "One", "Two", "Three",  
    "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",  
    "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",  
    "Seventeen", "Eighteen", "Nineteen" };  
    private static String[] tens = { "", "", "Twenty", "Thirty", "Forty",  
    "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };  
  
    public static String ConvertAmount(double amount)  
    {  
        try  
        {  
            Int64 amount_int = (Int64)amount;  
            Int64 amount_dec = (Int64)Math.Round((amount - (double)(amount_int)) * 100);  
            if (amount_dec == 0)  
            {  
                return Convert(amount_int) + " Only.";  
            }  
            else  
            {  
                return Convert(amount_int) + " Point " + Convert(amount_dec) + " Only.";  
            }  
        }  
        catch (Exception e)  
        {  
            // TODO: handle exception  
        }  
        return "";  
    }  
  
    public static String Convert(Int64 i)  
    {  
        if (i < 20)  
        {  
            return units[i];  
        }  
        if (i < 100)  
        {  
            return tens[i / 10] + ((i % 10 > 0) ? " " + Convert(i % 10) : "");  
        }  
        if (i < 1000)  
        {  
            return units[i / 100] + " Hundred"  
                    + ((i % 100 > 0) ? " And " + Convert(i % 100) : "");  
        }  
        if (i < 100000)  
        {           return Convert(i / 1000) + " Thousand "  
                    + ((i % 1000 > 0) ? " " + Convert(i % 1000) : "");  
        }  
        if (i < 10000000)  
        {  
            return Convert(i / 100000) + " Lakh "  
                    + ((i % 100000 > 0) ? " " + Convert(i % 100000) : "");  
        }  
        if (i < 1000000000)  
        {  
            return Convert(i / 10000000) + " Crore "  
                    + ((i % 10000000 > 0) ? " " + Convert(i % 10000000) : "");  
        }  
        return Convert(i / 1000000000) + " Arab "  
                + ((i % 1000000000 > 0) ? " " + Convert(i % 1000000000) : "");  
    }  

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM