Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Whan i used Convert.double() it brought an error 'System Convert' does not contain a definition for 'Double'
i had to change to Convert.Todouble() but it Still brings up the same error of "Input string was not in a correct format"

What is really the problem. Can u still provide help. See the code below how i did.
C#
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
{
double Balance;
double Amount1 = Convert.double(this.Amount.text);
double Amount2= Convert.double(this.AmountPaid.text);
Balance = Amount1 - Amount2;
}
Posted
Updated 15-Oct-19 7:49am
v2
Comments
walterhevedeich 21-Jul-11 5:52am    
Paste the exact code. There's no Convert.double, from what I know.
[no name] 15-Oct-19 13:42pm    
If taking user input, you need to validate that the input you received is numerical only, and if the values are in fact doubles. And you can do this with Double.TryParse, see my answer.

C# is case sensitive. It should be Convert.ToDouble not Convert.double if you want to use this particular calling convention. A better method would be to use the TryParse method which lets you convert the value if it's in an acceptable format, without triggering an exception if it's not. Try rewriting this method like this:
C#
double balance;
double amount1;
double amount2;

if (!double.TryParse(Amount.Text, out amount1))
{
  return 0;
]
if (!double.TryParse(AmountPaid.Text, out amount2))
{
  return 0;
}

balance = amount1 - amount2;
One thing though - you have declared balance locally, and you aren't doing anything. Effectively this method is performing a calculation for no reason right now.
 
Share this answer
 
v2
Comments
ssempijja 21-Jul-11 6:27am    
Compiler Error Message: CS0117: 'System.Convert' does not contain a definition for 'Todouble'

Source Error:



Line 153://double Amount2= Convert.ToDouble(this.AmountPaid.text == "" ? 0 : this.AmountPaid.text);
Line 154://Balance = Amount1 - AmountPaid1;
Line 155: double Amount1 = Convert.Todouble(Amount.text);
Line 156: double Amount2= Convert.Todouble(AmountPaid.text);
Line 157: Balance = Amount1 - Amount2;
Pete O'Hanlon 21-Jul-11 6:37am    
And where did I say that there was a Convert.Todouble. It's Convert.ToDouble - that's why I said C# was case sensitive.
Try:
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
   {
   double Balance;
   double Amount1 = Convert.ToDouble(this.Amount.Text);
   double Amount2 = Convert.ToDouble(this.AmountPaid.Text);
   Balance = Amount1 - Amount2;
   }
Or:
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
   {
   double Balance;
   double Amount1 = Double.Parse(this.Amount.Text);
   double Amount2 = Double.Parse(this.AmountPaid.Text);
   Balance = Amount1 - Amount2;
   }
If your users can enter non-numeric data, then you need to look at TryParse instead:
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
    {
    double Balance;
    double Amount1;
    double Amount2;
    if (Double.TryParse(Amount.Text, out Amount1) && Double.TryParse(AmountPaid.Text, out Amount2))
        {
        Balance = Amount1 - Amount2;
        }
    }
 
Share this answer
 
Try this


C#
using System.Text.RegularExpressions;
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
{
  Regex regex = new Regex("^[0-9]*$");
  if (regex.IsMatch(this.AmountPaid.Text) && !string.IsNullOrEmpty(this.AmountPaid.Text.Trim()))
            {
                double Balance;
                double Amount1 = Convert.ToDouble(this.Amount.text);
                double Amount2 = Convert.ToDouble(this.AmountPaid.text);
                Balance = Amount1 - Amount2;
                
            }
}
 
Share this answer
 
Comments
ssempijja 12-Aug-11 2:50am    
Regex regex = new Regex("^[0-9]*$");

The issue is with (^[0-9]*&"); the actually the code compiler remains at that location does not proceed but when i change ("^[0-9]*$") to ("[0-9].")it works but with an error after that the invalid data but it goes on calculating. What should i do?
Hi,
use this
C#
Private void AmountPaid_TextChanged(object sender, system.EventArgs e)
{
double Balance;
try
{
double Amount1 = Convert.ToDouble(string.Format("{0:0.00}", Amount.text));
double Amount2= Convert.ToDouble(string.Format("{0:0.00}", AmountPaid.text));
}
catch(Exception)
{
}
Balance = Amount1 - Amount2;
}
 
Share this answer
 
v3
Comments
shefeekcm 21-Jul-11 6:17am    
check this.
ssempijja 21-Jul-11 7:00am    
This is the error am getting:
================================
Compiler Error Message: CS0117: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'text'

Source Error:

Line 152: try
Line 153: {
Line 154: double Amount1 = Convert.ToDouble(string.Format("{0:00}",Amount.text));
Line 155: double Amount2= Convert.ToDouble(string.Format("{0:00}",AmountPaid.text));
Line 156: }


Source File: e:\KSDAHC\App_Code\Accounts\AddAccountsPage.Controls.cs Line: 154

====================================
these are the declarations stameents am using

using Microsoft.VisualBasic;
using BaseClasses.Web.UI.WebControls;
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseClasses;
using BaseClasses.Data;
using BaseClasses.Utils;
using ReportTools.ReportCreator;
using ReportTools.Shared;
Convert.ToDouble Method needs to know how to information's about the decimal and group separator. If this separators are different then the one used by default, u need to indicate the proper culture or create the custom culture for that conversion.

Then you should use the following method to convert it http://msdn.microsoft.com/en-us/library/9s9ak971.aspx[^]

At the end of the indicated page, there is also a nice example of how to achieve that.

Hope it helps

Cheers
 
Share this answer
 
You will get this error Amount.text or AmountPaid.text is empty. To avoid this error you have to check the empty before assigning and you have to Convert.ToDouble.
double Amount1 = Convert.ToDouble(this.Amount.text == "" ? 0 : this.Amount.text);
double Amount2= Convert.ToDouble(this.AmountPaid.text == "" ? 0 : this.AmountPaid.text);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900