Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how is it possible to convert a string 0,1234 to an integer that I can store in an SQL table that has an INT as field

What I have tried:

_sAmount = dr[13].ToString().Trim().Replace(",",".");
                    _Amount = Convert.ToInt32(_sAmount,4);
Posted
Updated 24-Oct-17 23:18pm
Comments
Andy Lanng 25-Oct-17 5:16am    
Do you want both ints or just the non-zero?

Replacing the comma with a dot won't work: that doesn't create an integer - it creates a a floating point number: "0.1234" which will not parse as an integer.

You could use double.TryParse instead of Convert.ToInt32 and then cast that to an integer, but that will just give you zero, as the .1234 part will be discarded.

I'd look at my data source, and try to work out exactly what it is supposed to hold: why does your DataTable column hold numbers like "0,1234"? Is that supposed to be two comma delimited numbers? Or a single, badly separated number "01,234"? Or what? Do you even have the right column? We can;t tell, and probably you can't either!

Start with the data, and work out what you have: just trusting it and converting it blindly will give you huge problems down the line.
 
Share this answer
 
If it's 0.1234 then an int won't be precise enough

Try double.parse(s)
 
Share this answer
 
that works but also I have numbers between

9999.9999 and -9999.9999

so negative and after the comma numbers..
how to convert that string into a sql data base.
In the database I use Numeric (10,5) witch works but how to convert them from my code c# from a string to a numeric??

dominick
 
Share this answer
 
Comments
Patrice T 13-Nov-17 14:54pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900