Convert text to number in C# to write to DB

Asked

Viewed 995 times

1

I’m using this conversion to get the string from txtvalor.text and turn it into such a value: 1.500,80

curso.Valor = double.Parse(txtValor.Text);

Example I type 1,500.80. And I would like you to write to the database as well, in the Mysql database.

  • So how? With the dot and the comma?

  • What example string is in txtValor.Text ?

  • 1st thanks for answering me. I would like to pass a value in currency to the bank. type 1.440.30. I’ve tried double, decimal and float, but none of them records the commas or the dots.

  • But you tried these guys in the bank or in code Behind there? Post your table structure (aa you tried);

2 answers

2


Avoid using text fields in the BD to store numerical values. Besides being a bad practice (except if there is a strong goal for this) increases the size of the database and makes research very difficult. It also causes performance drops. To save the value reported in your question (1,500.80), after converting in C# to DOUBLE (I advise you to use DECIMAL instead of DOUBLE), save the value in a NUMERIC field in Mysql.

  • 1

    Good recommendation. But today I have seen a lot of people use numeric fields to record texts. For example, phone, zip code, CPF, numeric codes. These fields are texts that only accept numeric digits but are not numeric fields. They are not used to do accounts. but the best recommendation is to use decimalin place of double. It amazes me that you still have a programmer who uses double for monetary values.

  • Well remembered @bigown. Fields such as telephone, CPF and CNPJ for example, really need a text field, although the stored value is numerical. One of the arguments for this is that these values can be preceded by 0 (zero), which would cause inconsistency if saved in a field of the numeric type.

0

Formatting is part of a visualization aspect (View), ie, the most reusable way would be to write in the database the corresponding numerical value and format in the best way on each of the application fronts.

But if you really want to force write, change the field type in the BD to scan (or other textual type), format in the application and then record. When you recover the value it already turns formatted.

  • 1

    -1 for using VARCHAR or text field to store numeric value in BD.

  • E. Thomas, I did NOT RECOMMEND using text field for values in BD. My first paragraph suggests just the opposite. The second paragraph is a way to make the recording possible, even if it is a bad pattern to follow. We do not know all requirements and scope, we only opine to help.

Browser other questions tagged

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