What Does the Underscore Mean in Literal Numbers

What does the underscore mean in literal numbers?

Underscores are ignored. You can put them in to make them more readable.

What do underscores in a number mean?

With Python 3.6 (and PEP-515) there is a new convenience notation for big numbers introduced which allows you to divide groups of digits in the number literal so that it is easier to read them.

Examples of use:

a = 1_00_00  # you do not need to group digits by 3!
b = 0xbad_c0ffee # you can make fun with hex digit notation
c = 0b0101_01010101010_0100 # works with binary notation
f = 1_000_00.0
print(a,b,c,f)

10000

50159747054

174756

100000.0

print(int('1_000_000'))
print(int('0xbad_c0ffee', 16))
print(int('0b0101_01010101010_0100',2))
print(float('1_000_00.0'))

1000000

50159747054

174756

100000.0

A = 1__000  # SyntaxError: invalid token

What do the underscores mean in a numeric literal in C#?

This is a new feature of C# 7.0 and it is known as digit separator. The intent is to provide better and easier readability. It is mostly useful when writing numbers that are very long and hard to read in source code. For example:

long hardToRead = 9000000000000000000; 

// With underscores
long easyToRead = 90000_00000_00000_0000;

It is totally up to the programmer on where to place the underscore. For example, you may have a weird scenario like this:

var weird = 1_00_0_0_000_0000000_0000;  
public const decimal GoldenRatio = 1.618_033_988_749_894_848_204_586_834_365_638_117_720M;

Some Notes

As soon as you compile your code, the compiler removes the underscores so this is just for code readability. So the output of this:

public static void Main()
{
long easyToRead = 90000_00000_00000_0000;
Console.WriteLine(easyToRead);
}

will be (notice no underscores):

9000000000000000000

<==Try Me!==>

Here is a discussion about when this feature was requested if you are interested. Some people wanted the separator to be blank spaces, but looks like the C# team went with underscores.

How Numeric literal with underscore works in java and why it was added as part of jdk 1.7

This is used to group the digits in your numeric (say for example for credit card etc)

From Oracle Website:

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can
use an underscore character to separate digits in groups of three,
similar to how you would use a punctuation mark like a comma, or a
space, as a separator.

To conclude, it's just for a sake of readability.

Why _(underscore) ignored in output?

It's not ignored in the output; it's ignored in the source code. The underscores are a convenience to make large number literals in code easier to read; the literal is still an integer, and integers don't contain underscores. You could always use a string of course:

a := "1_00_000"
fmt.Println(a)

Underscores as separators were added as a new feature in Go 1.13: https://golang.org/doc/go1.13#language

Noob Question: What is the _ in number literals in Kotlin. Like what the hell is 1_100?

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

Looks like Kotlin just inherited this feature from Java.

Ada Numeric Literals and Underline

It's the same reason for, say, commas (,) in currency or [other large] numbers: grouping.
Thus:

Million : Constant:= 1_000_000;

Furthermore, you could use it in conjunction with base-setting as a set-up for masking:

Type Bit is Range 1..8;
SubType Byte is Interfaces.Unsigned_8;
Type Masks is Array(Positive Range <>) of Byte;

Mask_Map : Constant Masks(Bit):=
(
2#0000_0001#,
2#0000_0010#,
2#0000_0100#,
2#0000_1000#,
2#0001_0000#,
2#0010_0000#,
2#0100_0000#,
2#1000_0000#
);

Then perhaps you would use Mask_Map and bits together with or, and, and xor to do bit-manipulation. The above method may seem a bit more work than the simple definition of a lot of constants and directly manipulating them, but it is more flexible in that you can later change it into a function and not have to change any client-code, that could further be useful if that function's result was a parametrized integer, where bit has the definition 1..PARAMETER'Size.

Underscores in numeric literals in scala

Note that starting Scala 2.13, underscore is accepted as a numeric literal separator:

val x = 1_000_000
// x: Int = 1000000
val pi = 3_14e-0_2
// pi: Double = 3.14


Related Topics



Leave a reply



Submit