Conversion of a variable with replace to decimal

Asked

Viewed 847 times

3

I’m having a little doubt, I pass a value 0,05 decimal for a variable A of the kind string. After this variable receives this value, it does the conversion of , to point ., which remains 0.05

After I do this replace, I convert the data to the type Decimal, my variable B that receives the variable A stays inteira 5.

 var openingCostValue = Convert.ToDecimal(model.openingCost.Replace(",", "."));

What I want is for you to keep the value that was converted to replace.

  • Can you put more code? From the beginning where you take the decimal value, you may have an easier way to solve this.

  • No need

  • So ta, good luck.

  • No need to replace, just direct convert. Just be careful with internationalization issues

  • true without replace

  • @Richarddias, the problem if n replaces, the moment it saves in my database, if the value is comma, SQL server leaves the integer value

  • Your data type in the database is numeric and in your code is string?

  • Yes, and convert to numeric type and send.

Show 3 more comments

3 answers

6


The right way to do this is to try parse taking into account the culture (there are scams that will only work by coincidence). If you had more information in the question I could give a more specific example. I will give a generic example:

decimal.TryParse("0,05", NumberStyles.Number, new CultureInfo("pt-BR"), out valor)

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Data and presentation is something different. Depending on what you need it may be that the solution is not even this one. I answered what was asked.

Eventually it is possible not to use the mechanism of try but you can only do this if you are sure that the conversion will always be successful. If the data comes externally, or from some component that cannot guarantee it, it cannot be certain.

1

Your method is returning whole because you have not defined the culture.

The Brazilian standard is "," Soon, if you pass "." the function will not recognize and return 5

Examples:

var valor = decimal.Parse("0,05", new CultureInfo("pt-BR")); Return: 0.05

var valor = decimal.Parse("0.05", new CultureInfo("pt-BR")); Return: 5

In your code, just don’t use the Replace

var openingCostValue = Convert.ToDecimal(model.openingCost); exit will be 0.05

If you want the comma output

var openingCostValue = Convert.ToDecimal(model.openingCost).ToString().Replace(".", ",");

1

You must use the replace if you want to format the value for presentation to the user. Otherwise just convert direct.

A different case is whether this string comes the user. By default the tab . is to decimal and the , is for thousands.

Soon Convert.ToDecimal("0.05"); = 0,05 and Convert.ToDecimal("0,05"); = 5.

  • So, man. When I send the value, it’s 0.05. If I do Convert.Todecimal(), it gets the integer value (5). Any decimal value that is passed will always be separated by ','. So I used replace.

Browser other questions tagged

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