is a microblog
home archives search feed blogroll

Underscores In Numbers

In Java, you can use underscores in number literals to make them easier to read.

int x = 1_000_000;
double x = 1_000_000.14159;

I tried with all the programming languages currently installed on my machine. It fails in most languages, but it does work in Ruby.

x = 1_000_000

However, it’s not recommended to use them in large projects. It makes searching for constants in code harder. You might try searching for 1000000 not knowing that it was actually written as 1_000_000.

A better alternative is to use syntax highlighting to color groups of digits within a number.

Posted on 2018-01-18   #til     #java     #ruby  

← Next post    ·    Previous post →