is such thing like 1E+5 in Java to represent 100000?

W

www

Hi,

I need to use some int or double like 100000, or 100000.0. Sometimes the
number becomes even bigger. I hate to count those zeros. I am wondering
if a more visually form, like 1E+5, available in Java.

Thank you.
 
S

Stefan Ram

www said:
I need to use some int or double like 100000, or 100000.0. Sometimes the
number becomes even bigger. I hate to count those zeros. I am wondering
if a more visually form, like 1E+5, available in Java.

To get the int value »200000«

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( 2 * java.lang.Integer.valueOf
( 1 + java.lang.String.format
( "%0" + 5 + "d", 0 ) )); }}
 
S

Stefan Ram

www said:
I need to use some int or double like 100000, or 100000.0.
Sometimes the number becomes even bigger. I hate to count those
zeros. I am wondering if a more visually form, like 1E+5,
available in Java.

Obviously, »1E+5« also can be used in Java, but it has double
type. To get the int value: »( int )1E+5«, but this might
deviate from the intended value, when the decimal number can
not be represented by the binary floating-point type »double«.
 
B

Bent C Dalager

www said:
I need to use some int or double like 100000, or 100000.0. Sometimes the
number becomes even bigger. I hate to count those zeros. I am wondering
if a more visually form, like 1E+5, available in Java.

To get the int value »200000«

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( 2 * java.lang.Integer.valueOf
( 1 + java.lang.String.format
( "%0" + 5 + "d", 0 ) )); }}

I think the question is rather if you can write something like

int i = 1e5;
which you can't as such, but you can write
double d = 1e5;
or
int i = (int) 1e5;

Cheers
Bent D
 
P

Patricia Shanahan

Stefan said:
Obviously, »1E+5« also can be used in Java, but it has double
type. To get the int value: »( int )1E+5«, but this might
deviate from the intended value, when the decimal number can
not be represented by the binary floating-point type »double«.

All values of int are also exactly representable in double, so there
will be no rounding error.

The technique should not be applied to long, because some of the larger
long values are not exactly representable in double.

As a trick for easier writing of numeric literals I sometimes
temporarily put in commas for thousands grouping, and delete them as
soon as I've checked my typing.

Patricia
 
M

Mike Schilling

Patricia Shanahan said:
As a trick for easier writing of numeric literals I sometimes
temporarily put in commas for thousands grouping, and delete them as
soon as I've checked my typing.


There are some languages that allow underscores for this sort of grouping,
e.g. 1_000_000. A shame Java doesn't.
 
R

Roedy Green

As a trick for easier writing of numeric literals I sometimes
temporarily put in commas for thousands grouping, and delete them as
soon as I've checked my typing.

I have wished for an extension to allow _ in literals for eyeball
grouping. They would be ignored since some cultures group by fours.
 
O

Oliver Wong

Mike Schilling said:
There are some languages that allow underscores for this sort of
grouping, e.g. 1_000_000. A shame Java doesn't.

I tried sneaky stuff like...

int i = 1/**/000/**/000;
int j = 1\uxxxx000\uxxxx000; /*(for various values of \uxxxx)*/

.... but they didn't work either. Darn.

- Oliver
 
S

Stefan Ram

Oliver Wong said:
int i = 1/**/000/**/000;

I assume that the following expression is evaluated
at compile time:

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( 1 * 1000 * 1000 ); }}

/*
1000000
*/
int j = 1\uxxxx000\uxxxx000; /*(for various values of \uxxxx)*/

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( 1\u003000\u003000 ); }}

/*
1000000
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top