Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li>Money (salary): <code>Decimal</code>.</li> <li>Birthday: <code>DateTime</code>.</li> <li>Name: <code>String</code>.</li> <li>Social Security Number: <code>String</code>.</li> <li>Weight of the earth: <code>float</code> or <code>double</code>.</li> </ul> <h1>Explanation</h1> <h2>Money</h2> <blockquote> <p>The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding. <a href="http://msdn.microsoft.com/en-us/library/system.decimal.aspx" rel="nofollow noreferrer">- MSDN</a></p> </blockquote> <p>It also can't be an integer because you'll need a decimal point ($59.9) and integers cannot have one, you'd have to convert it, e.g. with a cast which will always round it up for you: <code>((int)7.001) == 8</code> is <code>true</code>.</p> <p>You won't use a string either. Text is just the wrong representation (quantifying money does not result in a list of characters, right?). And you'd also like to run some math, I'm sure, and you cannot do that with strings directly because it's not numeric. E.g. <code>2 + 2 = 4</code>. Try the same with strings: <code>"2" + "2" = "22"</code> (<code>+</code> is overloaded: it adds for numerics and concatenates for strings).</p> <p><strong>Edit: My opinion of this has changed!</strong>.</p> <p>Computers and floating points are notoriously complicated and even error-prone (if you do not know precisely what you're doing). I suggest to not use them for money or anything precise &amp; critical. </p> <p>I recommend to use an integer type that will not overflow (for arbitrary size use <code>BigInteger</code>) and use it to represent the lowest resolution you need. E.g. you're likely to be okay with representing dollars as cents, so <code>150</code> is how you would represent <code>1.5</code>. Away with rounding errors and scary IEEE standards! woohoo! plus computers are faster with integers so you would typically get better performance, especially if you can manage to use an <code>int</code>, i.e. you're <em>certain</em> it will not overflow.</p> <h2>Phone Numbers & Security Number</h2> <p>Phone numbers and security numbers are called numbers, but really they're just a string of digits, aren't they? at least that seems the common perception. Well that already tells you: use a string.<br /> You're also unlikely to use a phone number for mathematical operations? (although I do suppose summing up phone numbers would make one wild afternoon).</p> <h2>Birthday</h2> <p><code>DateTime</code> is the standard .NET type for dates, I'm sure there's no need to explain why, the name is self-explanatory.</p> <h2>Name</h2> <p>String for obvious reasons.</p> <h2>Weight</h2> <p><code>double</code> or <code>float</code> are used to this kind of things. It depends on how much precision you want. Double gives you more, but the trade-off is that it takes more memory. It only makes a real difference when you have tons of them. My rule of thumb is to go with doubles unless I actually need to use a single/float. That being said, from my experience, almost every game that has something like that (a gravity force value, a weight, and such) is usually a <code>float</code> and rarely a <code>double</code>. Sometimes the domain will give you a different rule of thumb while you're working in it.</p> <p>Differences between float &amp; double: <a href="https://stackoverflow.com/questions/2386772/difference-between-float-and-double">link</a> &amp; another <a href="https://stackoverflow.com/questions/1074474/should-i-use-double-or-float">link</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload