Decimal variable removing decimal places

Asked

Viewed 2,812 times

1

I have a variable in decimal which receives a value of a textbox, but when this variable receives the value with decimal places it removes the decimal place and stores the value without the same.

What happens:

  • Value of Textbox: 2.5;
  • Value of variable: 0;

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

I am not able to understand why this is happening. It gives insertion error in the database:

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

  • Let me understand better, what appears if you print the value of TextBox before recording?

  • Appears 2,5 - but if I insert 2,5 in POSTGRE at the time of insertion it error. Because no kind of postgre data accepts comma, but when I’m manipulating the textbox and I put it in. Replace(",",".") and send the value of txt pro Numeric and Numeric receives the value it already receives without the .

  • @Lucasvasconcelos you need to go through more details then. http://answall.com/help/mcve

  • 1

    Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

4

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

You can make the conversion this way:

using static System.Console;
using System.Globalization;

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I put two ways for you to see how it can be done. You can read the documentation to learn other ways to use the conversion properly.

  • 1

    I would like to know what is the mistake here to have a -1?

1

I had the 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 a , by point

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

What is the reason for this? the fields of the type (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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.