0

I need to count weight without rounding. Which is the best type for count weight float , double or decimal.

float   weight=0.333f;
double  weight=0.333;
decimal weight=0.333m;

My weight will be from 0.000 till 999.000;

Dhi
  • 157
  • 3
  • 11
  • What exactly do you mean by "rounding"? – BradleyDotNET Sep 25 '18 at 15:19
  • I read that double has some rounding problems – Dhi Sep 25 '18 at 15:19
  • So with double am i ok? – Dhi Sep 25 '18 at 15:20
  • 2
    When working with fixed *decimal point* (`3` digits after it in your case) choose `decimal` (it is your case). When working with fixed *significant digits* (e.g. `999.1`, `78.56`, `5.366`, `0.2563` - `4` digits) the choice is `double` – Dmitry Bychenko Sep 25 '18 at 15:21
  • 1
    do you only have 3 digits after the decimal for precision? If that is the case, then the best way to get 100% accuracy is to use an int – Sam I am says Reinstate Monica Sep 25 '18 at 15:21
  • 1
    If you want exact values, you'll need to use `decimal`. Otherwise, you'll run into precision issues. This is usually only a problem for something like currency. I can't imagine 13.9999999997 instead of 14 posing a problem for weight. – itsme86 Sep 25 '18 at 15:21
  • _My weight will be from 0.000 till 999.000_ Decimal then. – TaW Sep 25 '18 at 15:22
  • 1
    all 3 floats, doubles, and decimals will suffice for most practical purposes though, and are probably easier to work with than the int. `decimal` has the highest precision, if that's what you're looking for, and it's purpose is for money, which is very similar to your weight calculations. – Sam I am says Reinstate Monica Sep 25 '18 at 15:23
  • 1
    To give you an idea of the problem with mapping decimals to IEEE floating point numbers... `0.1d` isn't exactly `1/10`: `Enumerable.Repeat(0.1d, 10).Sum() == 1d` is `false`. This can lead to all sorts of accounting problems. – spender Sep 25 '18 at 15:23
  • Alternatively, you can use `int` in the range 0 - 999000. Add a decimal separator during the output. – Alexander Petrov Sep 25 '18 at 17:46

0 Answers0