Decimal variable removing the decimal places

1

I have a variable in decimal that receives a value from a textbox , but when this variable receives the value with decimal places it removes the decimal place and stores the value without it. p>

What happens:

  • TextBox value: 2.5;
  • Variable value: 0;

    variavel = Convert.ToDecimal(TextBox);
    variavel = 25;
    

I can not understand why this is happening. Gives database insertion error:

  

NpgsqlException was unhandled by user code ERROR: 42601: syntax error at or near "3"

    
asked by anonymous 27.01.2015 / 13:15

2 answers

4

If the error is occurring because a data was typed incorrectly, what is missing is a validation. You should avoid using any data without first making sure it is suitable for use. You can never rely on data entry, even more for web .

You can do the conversion this way:

using System.Console;
using System.Globalization;

public class Program {
    public static void Main() {
        decimal valor;
        if (decimal.TryParse("123.45", out valor)) {
            Console.WriteLine(valor);
        }
        if (decimal.TryParse("123,45", NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, new CultureInfo("pt-BR"), out valor)) {
            Console.WriteLine(valor);
        }
    }
}

See running on dotNetFiddle . I've put two ways for you to see how it can be done. You can read the documentation to learn other ways to use conversion appropriately.

    
27.01.2015 / 14:21
1

I had this same problem these days, in my case I used javascript with regular expression to correct the error as follows.

Remove all text points.

str = str.replace(/\./g, "");

changes to, per point

str = str.replace(",", ".");

What reason for this? the type fields ( Decimal , numeric ) of SQL SERVER do not accept , to specify the decimal places.

The methods below can help with some things.

// ----- Decimal Extensions -----

public static Boolean IsValidDecimal(this String numStr)
{
    Decimal Dummy;
    return Decimal.TryParse(numStr.Replace(".", String.Empty),
        NumberStyles.Float, new CultureInfo(1046, true), out Dummy);
}

public static Decimal StringToDecimal(this String numStr)
{
    return Decimal.Parse(numStr.Replace(".", String.Empty), 
        NumberStyles.Float, new CultureInfo(1046, true));
}

public static Decimal? StringToNullableDecimal(this String numStr)
{
    Decimal? DecVal = null;
    if (!numStr.IsNullOrWhiteSpace())
        DecVal = numStr.StringToDecimal();
    return DecVal;
}

// Converte um Decimal para um String com 2 casos decimais
public static String DecimalToString(this Decimal dec, Int32 scale = 2)
{
    return dec.ToString("#,##0.00000000".Substring(0, 6 + scale),
                new CultureInfo(1046, true));
}

public static String DecimalToString(this Decimal? dec, Int32 scale = 2)
{
    return dec.HasValue ? dec.Value.DecimalToString(scale): String.Empty;
}

public static String DecimalToSqlString(this Decimal dec, Int32 scale = 2)
{
    return dec.ToString("0.000000".Substring(0, 2 + scale),
                new CultureInfo("en-US", true));
}

public static String DecimalToSqlString(this Decimal? dec, Int32 scale = 2)
{
    return dec.HasValue ? dec.Value.DecimalToSqlString(scale) : "null";
}

 public static Boolean IsNullOrWhiteSpace(this String str)
{
    return String.IsNullOrWhiteSpace(str);
}
    
18.02.2016 / 13:50