Last active
August 2, 2024 19:42
-
-
Save vhogemann/1d6a9581fa8ec91ef3a5506885430304 to your computer and use it in GitHub Desktop.
Converte uma string em double
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Em resposta a https://x.com/zanfranceschi/status/1819413388360810601 | |
| /* | |
| O que eu estou fazendo aqui é converter a string em um array de char usando | |
| o método Reverse(), e fazendo cast direto de char pra int. Cada dígito vai | |
| corresponder ao seu valor ASCII. Os números na tabela ASCII começam em 0, com o | |
| valor decimal 48. Então eu só diminuo 48 do valor do caracter pra ter o valor | |
| numérico de 0 até 9. | |
| */ | |
| double parse(string value) { | |
| double sum = 0; | |
| int idx = 0; | |
| foreach (int c in value.Reverse()) { | |
| sum = sum + (c - 48) * Math.Pow(10,idx)); | |
| idx++; | |
| } | |
| return sum; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment