Amitabh Baidehi

Amitabh Baidehi

  • NA
  • 23
  • 2.2k

How to convert decimal value to striing type

Mar 2 2018 1:51 AM
I'm trying to show the Total amount into word
 
Following is the code i'm using :-
 
public void amountinwords()
{
string word = ConvertNumbertoWords(Convert.ToInt32(popopop.Text));
amountttiinwwords.Text = word;
}
 
public static string ConvertNumbertoWords(int number)
{
if (number == 0)
return "ZERO";
if (number < 0)
return "minus " + ConvertNumbertoWords(Math.Abs(number));
string words = "";

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

if ((number / 10000000) > 0)
{
words += ConvertNumbertoWords(number / 10000000) + " Crore ";
number %= 10000000;
}

if ((number / 1000000) > 0)
{
words += ConvertNumbertoWords(number / 1000000) + " MILLION ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += ConvertNumbertoWords(number / 1000) + " THOUSAND ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += ConvertNumbertoWords(number / 100) + " HUNDRED ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "AND ";
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;
}
 
 
but it is fetching error System.FormatException: Input string was not in a correct format.
because the value is in decimal form (example - 8452.3)
 
 
 
Can anybody help me with this 
 
Thanks in advance..

Answers (13)