-2

I'm having some trouble to convert data taken in my database to float numbers. Basically, the application is rounding one of my values to one decimal house. The table contains a set of fields with monetary values and one of these values te application keeps rounding. The strangest thing is that, despite the fact that there are more float values on my database, just one of them is rounded (valorAtualizadoAcordo). So, this is my query:

SELECT 
    idAcordo,
    tipoAcordo,
    valorAtualizadoAcordo,
    dataAtualizacaoAcordo,
    valorConfessadoAcordo,
    valorAcordo,
    valorEntradaAcordo,
    valorParceladoAcordo,
    quantidadeParcelasAcordo,
    taxaAcordo,
    atualizacaoTrAcordo,
    dataPrimeiraParcelaAcordo,
    dataUltimaParcelaAcordo,
    dataAssinaturaAcordo,
    dataAprovacaoAcordo,
    primeiraDataAbertoAcordo,
    dataProtocoloAcordo,
    tipoProtocoloAcordo,
    cadastroDspsAcordo,
    dataSolicitacaoAcordo,
    contabilizadoAcordo,
    dataLancamentoAcordo,
    contratoDevolvidoAcordo,
    dataDevolucaoAcordo,
    volumetriaAcordo,
    pagamentoRealizadoDataAcordo,
    contratoAntesAjuizamentoDataAcordo 
FROM 
    acordo 
WHERE 
    FK_idContrato = 46;

And this is my convertion:

ac.setValor(float.Parse(acd.Rows[i][5].ToString(), CultureInfo.InvariantCulture));

The value I'm trying to retrieve from the database is 325,360.69. I'm converting it to PT-BR format, what gives me "325360,7" (in Brazil, we use "," instead of "." to define the decimal floating point).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Patrick Pierre
  • 79
  • 1
  • 10
  • 2
    float has roughly 7 digits of precision. You need to use double instead. – Dave S Jun 02 '17 at 18:19
  • from the documentation on [`Single`](https://msdn.microsoft.com/en-us/library/system.single(v=vs.110).aspx): A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. This means that some floating-point operations may lack the precision to change a floating-point value. –  Jun 02 '17 at 18:21
  • I recommend you use **float.TryParse**, so you can check if the database's value is appropriate for float capacity. On the contrary you have to use **double**. –  Jun 02 '17 at 18:30
  • This seems like the kind of situation `decimal` was created for? – NetMage Jun 02 '17 at 18:30
  • If you are working with “monetary values” you should be using base-10 `decimal` not base-2 `float`s. – Dour High Arch Jun 02 '17 at 18:32
  • Hey, thank you guys... use decimal or double instead of float really did the job. I didn't know of the limit of precision of float numbers in C#. Anyway, thanks, guys. – Patrick Pierre Jun 02 '17 at 18:58

1 Answers1

0

Not sure why you think making a string from a float and back is a good idea, but I would start by dropping the ToString. The use a cast or a real conversion method like convert.ToFloat:

float f = (float)acd.Rows[i][5];

Or:

float f = Convert.ToFloat(acd.Rows[i][5]);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325