Problem with string to decimal conversion c#

Asked

Viewed 475 times

0

Hello, I’m having a problem with string to decimal conversion, as demonstrated by the following code:

string valor = "0,01";
CultureInfo _provider = new CultureInfo("pt-BR");
decimal valorTratado = Convert.ToDecimal(valor, _provider);

The variable Treaty value should be with the value 0.01 but it is with the value 1, someone knows why this happens?

  • For me it’s okay: https://dotnetfiddle.net/n68roP

  • always worked for me, but from a time started to happen this, is it some configuration of the visual studio? or other type of configuration? when I deploy in production is ok, but local happens this.

  • Replace the point with the comma before moving to the ToDecimal.

  • I would just like to find out why this code has always worked and today it no longer works locally, I am with visual studio 2017 15.3.3

  • @Alexssandrorochaaquino Probably someone tampered with the computer’s location settings and switched the comma separator to a point (or any other).

  • The location settings were really wrong, it was in the Brazilian Portuguese language but the number settings as American English. by resetting settings to Portuguese back to normal.

Show 1 more comment

1 answer

-1

Alexssandro,

Your code should work without problem, I tested the same:

inserir a descrição da imagem aqui

This problem may be with your computer’s location setting. Your code is right.

Browser other questions tagged

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