3

Im using this

this is the data type im sending in the cmd:

NpgsqlTypes.NpgsqlDbType.Real;

here im cheking that is a numeric value:

   public bool IsNumeric(String value){
        try{
            float.Parse(value);
            return true;
        }
        catch(Exception e){
            return false;
        }
    }

And here is the alter table:

ALTER TABLE "Medicine" ADD COLUMN "Price" real; ALTER TABLE "Medicine" ALTER COLUMN "Price" SET NOT NULL;

The table data type is Real too, thats how i can save "float" cuz it seen that PostgreSQL doesnt have a float. Ive tried with double, money, numeric, and still doesnt work. The numbers is inserted in the table, but w/o the ".", for example, i writte 12.34 and 1234 is inserted. Already checked everything when debugging.

How can i solve this?

1
  • postgresql has two floating point types: real and double precision. Try select 1::double precision, 1::real; Commented Jun 15, 2012 at 17:28

1 Answer 1

2

First of all your isNumeric function is a bad way to check if a string value is numeric, this is a terrific use of try-catch construct ...

You should check if the string is numeric with a regular expression like the following :

public static bool IsNumeric(string value)
{
    bool match;
    //regular expression to match numeric values
    string pattern = "(^[-+]?\\d+(,?\\d*)*\\.?\\d*([Ee][-+]\\d*)?$)|(^[-+]?\\d?(,?\\d*)*\\.\\d+([Ee][-+]\\d*)?$)";
    Regex regEx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
    match = regEx.Match(value).Success;
    return match;
}

Then to insert a REAL value into the table you should convert it into the Single .NET datatype, take a look at the table in this question answer .

Sign up to request clarification or add additional context in comments.

3 Comments

I think Double.TryParse is a better way to check if a string is actually a decimal number...
I think that using a try-catch construct when it is not really needed is a bad solution.
May i know why is it THAT terrific? :/ i saw this in this same forum.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.