Click here to Skip to main content
15,890,845 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: C# Decimal Arithmetic Pin
obermd13-Jan-21 5:37
obermd13-Jan-21 5:37 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 8:19
trønderen13-Jan-21 8:19 
GeneralRe: C# Decimal Arithmetic Pin
Richard Deeming12-Jan-21 21:41
mveRichard Deeming12-Jan-21 21:41 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 2:40
trønderen13-Jan-21 2:40 
GeneralRe: C# Decimal Arithmetic Pin
Super Lloyd12-Jan-21 20:26
Super Lloyd12-Jan-21 20:26 
GeneralRe: C# Decimal Arithmetic Pin
Daniel Pfeffer12-Jan-21 20:56
professionalDaniel Pfeffer12-Jan-21 20:56 
GeneralRe: C# Decimal Arithmetic Pin
Kiriander12-Jan-21 22:01
Kiriander12-Jan-21 22:01 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 3:26
trønderen13-Jan-21 3:26 
My guess is one of two: Either, the application calls for the higher precision (almost 29 decimal digits for decimal, less than 17 decimal digits for double), but the numeric range is sufficiently large. This level of precision is rarely needed, except in some extreme scientific calculations.

Or, the application calls for full control of roundoff errors, with exact value representation of e.g. decimal fractions, rather than a binary approximation. This is a common requirement in e.g. accounting systems. (An alternative to decimal is of course to use a long value to represent an integer number of millicents or whatever the required precision of the transactions. Using decimal will usually require fewer application code lines, as you can deal directly with the unscaled amounts.)

When I explain programming to beginners (and also to some at intermediate level needing some correction...), I know from experience that for some, understanding the principal difference between int and float is difficult. So I rather call it 'counts' and 'measurements'. The price of an apple is not a measurement, it is a count of the pennies you have to pay. So we use an int - int is a synonym for a count. If you weigh that apple, you do not count the grams, or milligrams, or micrograms - you will not get a measurement exactly right. So we use a float - float is a synonym for a measurement value. Even if you claim that the weight of the apple is exactly 123.456 grams, it nevertheless is a measurement, calling for a float, not a count of milligrams.

Making beginners understand the difference between a count and a measurement is far easier than to explain int and float. In that framework, I really would like to consider decimal as a "counting" value, with the extension that it lets us count not only Euros or dollars, but even cents and tenths of a cent. With BCD, this was very explicit at the representation level as well; with decimal, it is blurred. C# documentation classifies it as a 'floating point numeric type'. Yet, decimal is functionally like BCD values, rather than as measurement float / double. Also by implementation, it is a scaled integer (i.e. counting) value.

I think using decimal for measurement values is an abuse. If you really need more than the 17 digits of a double, then you should go for one of the 'infinite precision' libraries provided for almost any language. (Writing one is not that difficult - I made one myself in my student days.) If you measure the dimension of the universe down to the millimeter, you are still making a measurement, not a count.

And if the range of decimal is not sufficient - e.g. if you want to count all the atoms in the universe - and decimal doesn't provide a sufficient numeric range, then you should go for a software 'infinite integer range' library. (I never saw one, but writing one is almost down to the trivial level, if you really have the need! Biggest problem is formatting the number as text!)
GeneralRe: C# Decimal Arithmetic Pin
BernardIE531713-Jan-21 7:02
BernardIE531713-Jan-21 7:02 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 7:40
trønderen13-Jan-21 7:40 
GeneralRe: C# Decimal Arithmetic Pin
BotReject18-Jan-21 3:59
BotReject18-Jan-21 3:59 
GeneralBacon jam Pin
Mike Hankey12-Jan-21 9:25
mveMike Hankey12-Jan-21 9:25 
GeneralRe: Bacon jam Pin
Greg Utas12-Jan-21 9:35
professionalGreg Utas12-Jan-21 9:35 
GeneralRe: Bacon jam Pin
trønderen13-Jan-21 3:35
trønderen13-Jan-21 3:35 
GeneralGlory be! We're getting vaccinated! Pin
Cp-Coder12-Jan-21 6:21
Cp-Coder12-Jan-21 6:21 
GeneralRe: Glory be! We're getting vaccinated! Pin
Maximilien12-Jan-21 6:32
Maximilien12-Jan-21 6:32 
GeneralRe: Glory be! We're getting vaccinated! Pin
Cp-Coder12-Jan-21 6:34
Cp-Coder12-Jan-21 6:34 
GeneralRe: Glory be! We're getting vaccinated! Pin
Chris Maunder12-Jan-21 6:56
cofounderChris Maunder12-Jan-21 6:56 
GeneralRe: Glory be! We're getting vaccinated! Pin
Maximilien12-Jan-21 7:03
Maximilien12-Jan-21 7:03 
GeneralRe: Glory be! We're getting vaccinated! Pin
Gerry Schmitz12-Jan-21 8:32
mveGerry Schmitz12-Jan-21 8:32 
GeneralRe: Glory be! We're getting vaccinated! Pin
Chris Maunder12-Jan-21 10:54
cofounderChris Maunder12-Jan-21 10:54 
GeneralRe: Glory be! We're getting vaccinated! Pin
OriginalGriff12-Jan-21 9:05
mveOriginalGriff12-Jan-21 9:05 
GeneralRe: Glory be! We're getting vaccinated! Pin
Cp-Coder12-Jan-21 10:13
Cp-Coder12-Jan-21 10:13 
GeneralRe: Glory be! We're getting vaccinated! Pin
Vivi Chellappa12-Jan-21 10:14
professionalVivi Chellappa12-Jan-21 10:14 
GeneralRe: Glory be! We're getting vaccinated! Pin
stoneyowl213-Jan-21 4:32
stoneyowl213-Jan-21 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.