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.
Stefan Ram - 11 Jul 2007 17:04 GMT
>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 ) )); }}
Bent C Dalager - 11 Jul 2007 17:23 GMT
>>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
[quoted text clipped - 8 lines]
> ( 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

Signature
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Stefan Ram - 11 Jul 2007 17:10 GMT
>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«.
Patricia Shanahan - 11 Jul 2007 17:28 GMT
>> I need to use some int or double like 100000, or 100000.0.
>> Sometimes the number becomes even bigger. I hate to count those
[quoted text clipped - 5 lines]
> 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
Mike Schilling - 12 Jul 2007 02:25 GMT
> 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.
Oliver Wong - 12 Jul 2007 22:29 GMT
>> 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.
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
Stefan Ram - 12 Jul 2007 22:40 GMT
>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
*/
Roedy Green - 12 Jul 2007 02:51 GMT
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 12 Jul 2007 02:50 GMT
>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.
see http://mindprod.com/jgloss/literals.html
see http://mindprod.com/jgloss/conversion.html
double -> String
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com