Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way of writing large numeral literals in C#

I have a method like this:

Prefix GetPrefix(decimal value)
{
    if(value > 11000000000000000000)
        return Prefix.CosmicBig;
    if(value > 1000000000000000)
        return Prefix.ReallyBig;
    if(value > 3000000000000)
        return Prefix.Big;
    if(value > 50000000)
        return Prefix.Okay;
    if(value > 750000)
        return Prefix.MostlyNormal;
    if(value > 750000)
        return Prefix.SoSo;
    if(value > 750)
        return Prefix.Small;
    return Prefix.MiserablySmall;
}

The exact values are not important. What matters is that they are sometimes changed (the prefixes are used for drawing and some text areas change sizes in development). I'm looking for a way of writing these literals in a way that's easily readably by a human changing it, without having to count all the zeroes. A separator would be nice. I thought about writing 11 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, but that's only barely more manageable. Using Math.Pow() does it a little better, but I'm not comfortable with using such calculations to define constants.

like image 444
John NoCookies Avatar asked Mar 01 '13 09:03

John NoCookies


People also ask

What is a numeric literal in C?

A numeric literal is a character string selected from the digits, the plus sign, the minus sign, and the decimal point. Numeric literals may contain up to 18 digits. This increases to 31 digits if 31-digit support ( -Dd31 ) is in effect.

What are the three types of numeric literals?

They are immutable and there are three types of numeric literal: Integer. Float python. Complex.

What are the 6 types of literals?

Primitive data types include int, byte, short, float, boolean, double, and char, whereas non-primitive data types include arrays, string, and classes. The primitive literals in java int, byte, short, float, boolean, double, and char represent certain signed integer values.

What are the types of numeric literals?

There are two types of numeric literals: integer and floating point. You can assign a numeric literal to any of the numeric data types or the money data type without using an explicit conversion function. The DBMS Server automatically converts the literal to the appropriate data type, if necessary.


2 Answers

Instead of 11000000000000000000 you can use 11e18. Use m to indicate that it is a decimal, so 11e18m.

like image 132
Sjoerd Avatar answered Oct 13 '22 11:10

Sjoerd


You can introduce extension methods for int:

750.Thousand();
5.Million();
100.Billion();

The implementation of these methods is simple:

public static int Thousand(this int value)
{
    return value * 1000;
}

public static int Million(this int value)
{
    return value.Thousand() * 1000;
}

// etc.

Make sure to return the appropriate data type for methods for bigger numbers:

public static long Billion(this int value)
{
    return value.Million() * 1000;
}

Unfortunatelly, you will have to write those methods for every integral or floating point type you want to support.

Having a full set of such extension methods would allow you to express your large numbers in relativly natural ways, even when it's not just all zeros at the end:

100.Billion() + 30.Thousand() + 300 // == 100,000,030,300

If you want to get fancy, you could even think about nesting them:

100.Billion(30.Thousand(300))

But I think that would lose some expressiveness, because people would wonder what the parameter means.

Still, implementation would look like this:

public static long Billion(this int value, long add)
{
    return value.Million() * 1000 + add;
}

Using these extension methods has one little downside: Your numbers are no longer compile-time constants. They are calculated at runtime. In the vast majority of cases, this shouldn't be a problem.

like image 38
Daniel Hilgarth Avatar answered Oct 13 '22 10:10

Daniel Hilgarth