Eugene Fedotov
Eugene Fedotov

Reputation: 354

How to represent large numbers nicely in source code for Java?

Instead of typing 65232, I want it easy to read like 65 232. There's supposed to be something I can insert to format it that way.

Upvotes: 6

Views: 1725

Answers (4)

Piyush Mittal
Piyush Mittal

Reputation: 1890

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.

The following example shows other ways you can use the underscore in numeric literals:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

You can place underscores only between digits; you cannot place underscores in the following places:

At the beginning or end of a number
Adjacent to a decimal point in a floating point literal
Prior to an F or L suffix
In positions where a string of digits is expected
The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:

float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
  = 999_99_9999_L;         // Invalid; cannot put underscores prior to an L suffix

int x1 = _52;              // This is an identifier, not a numeric literal
int x2 = 5_2;              // OK (decimal literal)
int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2;        // OK (decimal literal)

int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2;            // OK (hexadecimal literal)
int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number

int x9 = 0_52;             // OK (octal literal)
int x10 = 05_2;            // OK (octal literal)
int x11 = 052_;            // Invalid; cannot put underscores at the end of a number

Upvotes: 1

resueman
resueman

Reputation: 10613

You can use underscores instead of spaces, which will get close to what you want.

int i = 65_232;

This is assuming that you're using at least Java 7.

The compiler treats it as though the underscores weren't there, so you can place them (almost) anywhere you want inside of numeric literals, which gives you quite a bit of freedom in how you format your source code.

int XD = 0__0;

Upvotes: 8

Mureinik
Mureinik

Reputation: 311188

Java 7 introduced the ability to use an underscore (_) in numeric literals. It has no meaning and is just ignored by Java (e.g., 1_00, 10_0 and 100 all mean one hundred), but it's very convenient to use as a visual separator. In your case: int myInt = 65_232;.

Upvotes: 0

Caffeinated
Caffeinated

Reputation: 12484

Try something like this :

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols myNumber = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(myNumber);
System.out.println(formatter.format(bd.longValue()));

source

Upvotes: 0

Related Questions