views:

385

answers:

12

Does .Net have any built in constants for common numbers like million, billion etc?

EDIT: As has been suggested this was for readability reasons, rather than writing 1000000 or 1000000000. I know I can create my own just wanted to check that they didnt already exist before I did so.

+2  A: 

I'm going with no.

There are built in constants for the largest and smallest number that will fit in an int or long, for example, but no arbitrary constants between those points.

Dean J
+5  A: 

Why do you think million and billion are common numbers?

Alex Reitbort
er, yes in my application.
Si Keep
@Si: *your* application, hence the need for you to define *your own* constants ;)
Adam Robinson
+5  A: 

No,

There are a few constant avallable, like

  • double.Epsilon
  • Math.PI

But not for million and billion.

You can create your own constants

public const int OneMillion = 1000000;

I presume you are looking to transform a number like int value = 1000000 into a natural string like string value = "1 million".

That don't exist in .NET by default, but it could be coded easily or I presume you could find such code on Google.

For example, you could create an extension method on int that transform the number into a human natural language string: (this is cheap code from off the head)

public static string ToNaturalString(this int value)
{
    if (value == 1000000)
        return "1 million";
    else
        return value.ToString();
}
Pierre-Alain Vigeant
A little correction: it *is* possible to format a number like that, by using one or more commas directly *after* the number placeholder. Each comma scales by 1000 and then the result is rounded: 1000000.ToString("0,, 'million'") == "1 million"; 1500000.ToString("0,, 'million'") == "2 million"
Ruben
Its very sensitive to the current culture, so it is dangerous to rely on the comma although you could format into InvariantLanguage.
Pierre-Alain Vigeant
Specifically, remember that many cultures interchange the meaning of "." and "," in numbers. Is 100,000 an integral hundred thousand or a floating-point hundred? Depends on where you are.
David Thornley
+22  A: 

Sure, they are named 1e6, 1e9, etc... ;-)

Heinzi
Hey, I didn't know that. That's nice.
Pierre-Alain Vigeant
As with the other comment, while this is intended as a joke, you're listing *literals*, not *constants*.
Adam Robinson
@Adam: Sure, that's the point of the joke. :-) Thanks, though, for pointing it out, since this might not be obvious to everyone. @All: Note that the above literals return floating-point values, not integers.
Heinzi
+1 All this time I have been wasting zeros.
DancesWithBamboo
why have we not thought about this earlier... shame on us!
Nicolas Dorier
A: 

Yes

int million = 1000000;
int billion = 1000000000;

Just like 5, a, and -1, these are constants.

Dinah
No, those are *literals*.
Adam Robinson
@Adam Robinson: I'd say there's nothing more constant than a literal.
Dinah
double one = 1.5;Assert.That(one + one == 3);Q.E.D.
Krypes
@Dinah: Death and taxes come to mind. Nonetheless, as terms of art "constant" and "literal" are different.
Adam Robinson
@Adam Robinson: since we're splitting hairs, technically we could eliminate taxes and scientists may even one day cheat death, but 'a' will always be 'a' and 5 will always be 5.
Dinah
@Dinah: tongue in cheek, obviously. Nonetheless, it's incorrect to equate constants and literals. I don't think that a debate is particularly beneficial. That fact that you got four downvotes within the span of about a minute is probably evidence of that ;)
Adam Robinson
@Adam: noted, yet ironically, the identical humor got an equal number of upvotes in the same span and has yet to be downvoted. Baffling isn't it?
Dinah
@Dinah: It is; but to say it hasn't been downvoted would be incorrect ;)
Adam Robinson
@Adam: I don't know what's worse, that you are trying to lead me down the same rabbit hole or that part of me *really* wants to go there anyway ;) must.... resist....
Dinah
@Adam: Not necessarily. Case in point: a FORTRAN programmer passed 1 to a function, which was passed to another function and changed there, changing the value of 1 throughout the program. When he figured that out, he set something like ONE=1 (declaring ONE as integer, of course, since it's out of the I-N range), and everything worked fine. His conclusion: "Murphy was right. Constants aren't, and variables won't."
David Thornley
@David: You're telling me that I can reassign the value of a literal in FORTRAN? I can say 1=2?
Adam Robinson
+8  A: 
John Feminella
It's even more important to realize that numbers like million and billion only even have the *slightest* meaning in base-10.
Adam Robinson
3.14159 is not π, it's merely an approximation within an error of around 2.7e-6.
Joey
@ Johannes, with pi, an approximation is all you can ever get.
Gurdas Nijor
@Gurdas: Depends on your programming language/library. It might be an interesting exercise to provide a precise "Pi" constant in a language that supports lazy evaluation of irrational numbers...
Heinzi
@Gurdas: going along with what @Heinzi said, if your language supports lazy evaluation and/or symbolic evaluation, it's entirely possible to create expressions where the πs cancel out and you're left with an exact result.
rmeador
@Johannes: You are correct, of course. I was taking a little liberty with the specifics in an attempt to focus on what I thought was the central point: just because something is a literal doesn't mean it has any significance as a constant.
John Feminella
Okay, what is the exact value of pi, then? Oh, crap. We just used all the available atoms in the universe. Stack overflow.
David Lively
+12  A: 

I can see your point where a manifest constant in code like 1000000000 might have a readability problem "is that one hundred million or a billion". However, I'd create a constant that suited the particular situation rather than a generic constant. For example

#define BANKOFAMERICA_TARP_AMOUNT 1000000000.0; // One billion dollars
Bob Kaufman
+1 for making me laugh, then cry...
Chris Ballance
+1 for hilarity
Kevin Laity
that constant should be closer to 153000000000, according to this: http://en.wikipedia.org/wiki/Troubled_Asset_Relief_Program#Participants. I'm going to go prepare for doomsday now.
rmeador
excuse me, I did my math wrong. That should have been 163000000000. Note that this will not fit in a 32 bit integer.
rmeador
When I said "Dollars" I meant "Gold Sovereigns", but I figured the reference might be a bit vague... :)
Bob Kaufman
One *billion* dollars! <pinky>
John Feminella
+1 for "suited the particular situation", #define DEVELOPER_BILLION 1073741824 //2^30
Nicolas Dorier
+3  A: 

Even if there were such constant, the word "billion" means different things whether you're American or European.

In North America, a billion equals 1,000,000,000 = 10^9; in Europe, a billion equals 1,000,000,000,000 = 10^12. (A milliard = 10^9.)

Tenner
Clearly the Euro's are wrong ;) Millard? Isn't that a duck?
Adam Robinson
I don't know which bit of Europe you're referring to, but for everywhere I've been (including where I live in the UK) a billion is not 1,000,000,000,000! It's 1,000,000,000
joshcomley
Clearly you Yanks are wrong and don't know your Europeans!
joshcomley
@joshcomley: List of long-scale countries listed here: http://en.wikipedia.org/wiki/Long_and_short_scales#Current_usage
Tenner
@Tenner - But I'm European! You can't just generalise and say "Europe", that is misleading and just wrong
joshcomley
@joshcomley: Fair enough, but I was trying to put out a quick comment that got the general gist across in six characters instead of 600. Sorry if I offended you.
Tenner
@josh: Only a "European" would be offended by such a remark ;)
Adam Robinson
+1  A: 

I don't think we could have one on the grounds that there is a difference between a US Billion (one hundred thousand million) and a British (and others?) Billion (one million million)

Differences in short scale and long scale numbering

Rob Cowell
Couldnt there be 2 constants for billion, with different names?
Si Keep
Yes, that's possible I suppose
Rob Cowell
As mentioned *in the link you provided* a US billion is one thousand million, not one hundred thousand million.
David Winslow
Consider me suitably admonished for posting first, finding a suitable article second
Rob Cowell
+1  A: 

My favorite to use is:

public const float EleventyBillion = 110000000000;
Chris Ballance
Wouldn't eleventy billion be 110,000,000,000? What you listed was eleven billion.
Adam Robinson
@Adam Indeed, thank you sir.
Chris Ballance
+1  A: 

Somewhat tongue-in-cheek, but there's nothing stopping you putting extension methods on int:

public static int million(this int i)
{
    return i * 1000000;
}

in order to make

 int approximatePopulationOfUnitedStates = 300.million();

valid code...

AakashM
Oh, the horror!
Adam Robinson
in a strange way, I really like that.
Si Keep
It's worse than that, it's 350.million()!
JonB
300.Thousand().Million() == 300.Billion()
Adam Robinson
You could also add all sorts of other extensions, like `Tenths` and `Hundredths`. God save us.
Adam Robinson
A: 

could do something neat like have a library class

public class One
{
  public const int Thousand = 1000;
  public const int Million = Thousand * Thousand;
  public const int Billion = Million * Million;
  ...
}

Then when you go to use it it looks a bit neater

int myNumber = 72 * One.Million;

I must admit, I do this sometimes just for purely readability (however just have consts at the top of a class, not a dedicated library). Sometimes it's useful for numbers to have something like

// Add one to account for whatever
int countPlus1 = count + 1;

if you are referencing it a heap of times afterwards, it just makes code that little bit neater IMO. Also makes commenting easier, as it's easier to put a short comment at the start to understand why you are adding one to the variable all the time than just see random count + 1 code.

mrnye