Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

No. In general, constant values are best used for literals which have semantics outside of their value. For example, 3.14159 is a literal, but it's also a broadly important mathematical value, π (albeit an approximation).

By comparison, a number like 1,000,000 is simply a literal and doesn't necessarily have any general meaning other than its value, so there's not much worth in making it a built-in constant. In a particular application or context, however, it might be important -- perhaps 1,000,000 is the maximum number of ShippingContainers your tanker can hold or something similar. That's where you'd use a constant.

Why do you think million and billion are common numbers?

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();
}

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

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.

My favorite to use is:

public const float EleventyBillion = 110000000000;

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

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

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.

Yes

int million = 1000000;
int billion = 1000000000;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top