0

I need to calculate the NetAmount and IVA automatically from the TotalAmount, all works fine but the big problem is when the user for error digit a character in the field TotalAmount and when this happen shows an error and the program closes automatically without terminate all the process. We need that in the field TotalAmount also admit string character or find the way to accept any character.

if (field.Name == "InvoiceTotalAmount")
  {
    Field total = (Field)field.ParentDocument.Fields["InvoiceTotalAmount"]; //Field Total
    string totaltemp;
    double totaltemp1;
    totaltemp = total.Value;
    totaltemp1 = System.Convert.ToDouble(totaltemp);
    bool isNumeric = double.TryParse("1234567890", out totaltemp1);     
    if (isNumeric == true)
    //if (total.Value != "" )
    {
        Field iva = (Field)field.ParentDocument.Fields["InvoiceVatAmount"]; // IVA
        Field neto = (Field)field.ParentDocument.Fields["InvoiceNetAmount"]; // Netamount   
        double var1;
        double var11;
        string var4;
        var4 = total.Value;

        double var5 = System.Convert.ToDouble(var4);        

        var1 = 0.0087;
        var11 = 0.0013;

        var1 = (var5 * var1);
        var11 = (var5 * var11);

        string var3 = var1.ToString("C");
        string var12 = var11.ToString("C");

        neto.Value = var3;
        iva.Value = var12;

        neto.State = DataState.Ok;
        iva.State = DataState.Ok;
    }
    else
    {
    return false;
    }
}
return false;
Obed Silva
  • 19
  • 5
  • Looks good at a glance. You need to catch the exception, call `ToString` on it, save the result and paste it into an [edit]. –  Jul 13 '17 at 16:40
  • you want to accept only numbers? – MiOnIs Jul 13 '17 at 16:40
  • Hello, Yes, but we need that if the user enters a character by mistake, the program can admit it so that he can correct it. Currently when the user enters a character the program show an error and it closes automatically – Obed Silva Jul 13 '17 at 16:51
  • @Will where could put the exception, could you please help me in this. Thank you. – Obed Silva Jul 13 '17 at 18:05
  • Ok, first up, put a try/catch block around all your code ([docs](https://docs.microsoft.com/en-us/dotnet/standard/exceptions/how-to-use-the-try-catch-block-to-catch-exceptions)). That will let you deal with exceptions better in future. That said, you don't need an exception for your code to work. You need `double.TryParse()` ([docs](https://msdn.microsoft.com/en-us/library/3s27fasw(v=vs.110).aspx)). That will return `True` if it manages to read a number from the string and store it in the provided variable, or false otherwise (meaning you can tell the user) – Basic Jul 13 '17 at 22:34

1 Answers1

0

Following on from my comment above, you need to use Double.TryParse() to get a double from a string without dealing with exceptions (which are expensive in C# and should be avoided as part of normal program flow).

Here's how you use it....

string inputData = "123.456";
//string inputData = "Some invalid data";

double result;
var success = Double.TryParse(inputData, out result);
if (success) {
    // Use result as desired
} else {
    // Failed to parse input string
}

Note that since you're dealing with financial information, double is actually the wrong type to use.

Floating point numbers (including doubles) suffer from an accuracy issue. The reasons for this are complex and relate to the way that binary data is stored in memory to represent a number. Some examples of the problem can be found in the answers here

Even if you don't understand it all just make sure you use Decimal for currency.

Fortunately, Decimal offers exactly the same TryParse method...

decimal result;
var success = Decimal.TryParse(inputData, out result);

Do read Jon Skeet's article on the topic. It's a great explanation of the problem.

Basic
  • 25,223
  • 23
  • 108
  • 188
  • Hello, thanks for your advise. I try with 'decimal.TryParse' and working fine. Thansk again. – Obed Silva Jul 14 '17 at 16:52
  • Glad I could help. If this answers your question, please consider accepting using the tickmark on the left hand side. – Basic Jul 14 '17 at 17:29