Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
postgresql has two floating point types: real and double precision. Try select 1::double precision, 1::real; –  Clodoaldo Neto Jun 15 '12 at 17:28

1 Answer 1

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 .

share|improve this answer
1  
I think Double.TryParse is a better way to check if a string is actually a decimal number... –  zmbq Feb 26 '12 at 21:26
    
I think that using a try-catch construct when it is not really needed is a bad solution. –  aleroot Feb 27 '12 at 7:47
    
May i know why is it THAT terrific? :/ i saw this in this same forum. –  dbncourt Feb 28 '12 at 4:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.