Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float on PostgreSQL using c#

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?

like image 935
dbncourt Avatar asked Apr 21 '26 02:04

dbncourt


1 Answers

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 .

like image 169
aleroot Avatar answered Apr 22 '26 14:04

aleroot