C # which is the best data type for precision 3?

I want to keep the number with the next 0.000, which is the best data type to use.

Double?

Also, I assume that INT is simply out of the question?

+4
source share
12 answers

The decimal type is best for fixed-point math.

In C # Spec Section 4.1.7:

In contrast to float and double data types, decimal fractional numbers, such as since 0.1 can be represented exactly in decimal. in floating and double representations, such numbers are often infinite fractions, making these representations more prone to rounding errors.

+26
source

I am still a fan of the decimal number, it may take a little more space, but does not have the same rounding errors as float and double.

It really depends on what accuracy and accuracy you need, as well as the types of numbers you expect to store.

For example, if you work with money, rounding errors in float or real are likely to be unacceptable anyway.

Of course, if you fix it completely, depending on the required range of values, you can simply multiply by 1000 and save it as an integer type, for example, short.

Example rounding error from http://www.yoda.arachsys.com/csharp/floatingpoint.html

 double a = 0.65d; double b = 0.05d; Console.WriteLine("{0:r}", a + b); 

Please note that {0:r} is important when printing, otherwise .net will round the value and it will display as 0.7, although it is not equal to 0.7.

Over the course of several calculations, such rounding errors can be increased.

+6
source

If you are dealing with decimal numbers, and your specification requires precise accuracy for N decimal places, then your options are decimal or int / long with manual scaling (i.e., multiply and divide by 1000 as necessary when doing I / O and arithmetic). The latter is faster and more compact, the former is slower and wider, but the code is shorter and clearer. Of course, you can also encapsulate all the fixed-point template code in your own struct , which uses int / long under the hood.

Do not use float or double . They cannot represent many decimal places for sure. You can avoid this if you are always consistent, but as soon as you start doing arithmetic or comparisons, it can (and usually will) push you away because rounding errors accumulate.

+4
source

Based on your comment that yopu represents file size, consider storing bytes in an integer or long integer ... This completely fixes your problem, avoids the rounding errors inherent in converting 1 / 1024th from KB to double or float, and has to be closer to the real world dimension that you represent.

+3
source

float has 7 digit precision. double - precision of 15 digits.

so the float will be more than enough

+2
source

My advice: use float. For more accurate operation, use double and even more precise decimal values.

+2
source

Int is perfect for you. You will destroy the memory with decimal, and if you use ONLY 3 decimal places, you can always remember to multiply / divide by 1000.

In any case, it can be tedious if you use it for financial purposes - then still adhere to decimal places.

If you use it for some units, such as duration or length, int is just fine for you, because you can always go from seconds to milliseconds or from meter to millimeter.

It would be helpful if you would explain the desired use.

Also, from the information I see on MSDN, the decimal value is similar to floating point precision, but with greater precision and without rounding.

Next: decimal - 16 bytes, int - 4 bytes

In any case, your int divided by 1000 will have a range of only +/- 2,147,483

EDIT:

I read your comment and you want to use if for files? If so, I don’t see where the decimals come from ... Maybe they are only for display?

Anyway, go to Int64 here.

+1
source

The float will be better.

0
source

If for the file size you can change the size from kilobytes to bytes or from a smaller range (from meg to kb) and not have any floating and preserving the same accuracy.

Otherwise, double will be good.

0
source

The float is “accurate” up to about six decimal places, and the double is “exact” with much higher accuracy. However, none of them can accurately represent each value in 3 decimal places.

Most likely you will make good use of the float if you don't mind the number 0.123, which should be stored as 0.122999999 ... a little careful rounding should be enough to achieve what you want.

If you want to accurately represent all possible 3-decimal fractions, you better use an integer and multiply the values ​​by 1000. That is, the value 3.456 will be stored in an integer - 3456.

0
source

Do you want to keep the number this way or present ? Store with maximum precision and format it to # .000. Keeping your numbers is unlikely to be a storage issue, so keep using too much value.

As noted, integers may be suitable for things that can be reduced to integer values. (display GB, store bytes as int or bigint; show days, store seconds as bigint)

0
source

For the file size that you think you want to save, use a long (Int64) to store the number of bytes. Or use int if you know the files will be smaller than about 2 GB. You can convert this to a more readable form using a simple method .

0
source

All Articles