0

I'm working on some small projects and don't fully understand data types and their uses. Here are some things I'm wrestling with.

What data type would I use to represent:

  • a person's salary?
  • a person's date of birth?
  • a person's name?
  • a person's Social Security Number?
  • the number of dependents a person is going to claim on their taxes?
  • the weight of the Earth?

Are there any rock solid resources for data types?

p.campbell
  • 91,713
  • 61
  • 243
  • 314
chrisholdren
  • 273
  • 1
  • 3
  • 5
  • This sounds a bit like homework, can you add the `homework` tag to it? – JonH Oct 28 '11 at 19:13
  • 1
    You should find a tutorial online for your specific language, you will be able to find the basic info for int/float/string/bool very early in the lessons. – GodsCrimeScene Oct 28 '11 at 19:16

3 Answers3

3
  • Money (salary): Decimal.
  • Birthday: DateTime.
  • Name: String.
  • Social Security Number: String.
  • Weight of the earth: float or double.

Explanation

Money

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. - MSDN

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: ((int)7.001) == 8 is true.

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. 2 + 2 = 4. Try the same with strings: "2" + "2" = "22" (+ is overloaded: it adds for numerics and concatenates for strings).

Edit: My opinion of this has changed!.

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 & critical.

I recommend to use an integer type that will not overflow (for arbitrary size use BigInteger) and use it to represent the lowest resolution you need. E.g. you're likely to be okay with representing dollars as cents, so 150 is how you would represent 1.5. 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 int, i.e. you're certain it will not overflow.

Phone Numbers & Security Number

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.
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).

Birthday

DateTime is the standard .NET type for dates, I'm sure there's no need to explain why, the name is self-explanatory.

Name

String for obvious reasons.

Weight

double or float 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 float and rarely a double. Sometimes the domain will give you a different rule of thumb while you're working in it.

Differences between float & double: link & another link.

Community
  • 1
  • 1
MasterMastic
  • 19,099
  • 11
  • 59
  • 86
  • Thanks Ken, this was very helpful. Some of them are very straight forward like DateTime, but its the others ones that I was in question about. Is there a site for best practices? Or is just one of those common sense/ best judgement/ experience kind of things? – chrisholdren Oct 28 '11 at 19:42
  • @chrisholdren Common sense. you just need to know that string is just chars. int is a number without a decimal point, decimal, float, double - are with a decimal point. and stuff like that. than it just makes sense to use string for name, because a name is just chars. an age is (usualy) just a number so int. a price will have a decimal point so decimal/float/double. – MasterMastic Oct 28 '11 at 19:46
  • Thanks a lot I need some similar explanation about the financial data, – krachleur May 20 '15 at 15:16
  • @krachleur I'm a bit late :P (I've missed your comment notification), but I've updated my answer just now mainly to improve on financial data. If you're still working with financial data I strongly recommend to check it out. – MasterMastic Jan 25 '16 at 12:57
1

salary - use float or double; both represent decimal, the latter is double precision which you probably won't need since money usually only goes 2-3 decimal places
DoB - depends on language, most higher level languages have Date or DateTime object types, otherwise you might use a string or char array, or even create your own Date class
name - obviously string or char array
ssn - since is viewed as a string of characters (and also might have non-numeric characters '-') and not necessarily a value of magnitude, you can store this as a string or char array. Same goes for phone#
# of dependents - since can only be a whole number and never a decimal, use integer
weight - again can be represented by a decimal number, so float or double

Nick Rolando
  • 25,176
  • 13
  • 72
  • 111
  • The implementation on types is going to be specific to language and os. This is a broad general answer. If you would like to tell us the language in your question, we could be more specific. – Nick Rolando Oct 28 '11 at 20:45
0

Think about the values represented for each question.

A salary means money represented by dollars and cents (at least in the US) so you need some sort of decimal / floating type

Date of birth represents a simple date

Name represents a string

Social security number represents a string

Number of dependents for taxes is an integer type as it is a whole number

Weight of earth is again some sort of decimal / floating number

Is this homework? You also did not mention in what type of programming languages but either way these are types and generally consist of strings, dates, numbers, decimals, etc.

JonH
  • 31,157
  • 11
  • 79
  • 138