String format and conversion between data types

2 views
Skip to first unread message

Chatura Atapattu

unread,
Dec 8, 2010, 1:40:06 PM12/8/10
to coms4995pppp
I had two pretty basic questions:

1. Where can I find documentation on the format to provide in a print command?  I see that Console.OUT.print() takes a format and the variables, but using the basic C/Java style formats failed.

2. I'm trying to convert a long to a double.  However, the only way I found was doing something like this:

val longVal = 5;
val doubleVal = Double.parseDouble(longVal.toString());

There must be a simpler way to do this, but what is it?

Thanks!

- Chat 

Karthik Kumar Srivatsa

unread,
Dec 8, 2010, 1:47:26 PM12/8/10
to coms49...@googlegroups.com, Chatura Atapattu
1) Console.OUT.println is very similar to Sytem.out.println in java.
2) To typecast use:
val l: long = 5;
val d: double = l as double;

Karthik

Martha Kim

unread,
Dec 8, 2010, 1:47:54 PM12/8/10
to coms49...@googlegroups.com
Hi Chat,

1. Where can I find documentation on the format to provide in a print command?  I see that Console.OUT.print() takes a format and the variables, but using the basic C/Java style formats failed.

Looks like you want printf(), not print().  Here is the documentation for x10.io.Printer, the type for Console.OUT: http://dist.codehaus.org/x10/xdoc/2.1.0/x10/io/Printer.html
 
2. I'm trying to convert a long to a double.  However, the only way I found was doing something like this:

val longVal = 5;
val doubleVal = Double.parseDouble(longVal.toString());

There must be a simpler way to do this, but what is it?

Try this:

val doubleVal = Double.fromLongBits(longVal)

Chatura Atapattu

unread,
Dec 8, 2010, 1:51:11 PM12/8/10
to Karthik Kumar Srivatsa, coms49...@googlegroups.com
1. I meant printf.  I realized that I was trying to use print instead of printf.
2. Thanks.  Is that the only other way?

Chatura Atapattu

unread,
Dec 8, 2010, 1:55:38 PM12/8/10
to coms49...@googlegroups.com
On Wed, Dec 8, 2010 at 1:47 PM, Martha Kim <mar...@cs.columbia.edu> wrote:
Hi Chat,

1. Where can I find documentation on the format to provide in a print command?  I see that Console.OUT.print() takes a format and the variables, but using the basic C/Java style formats failed.

Looks like you want printf(), not print().  Here is the documentation for x10.io.Printer, the type for Console.OUT: http://dist.codehaus.org/x10/xdoc/2.1.0/x10/io/Printer.html

Correct, got it working.
 
 
2. I'm trying to convert a long to a double.  However, the only way I found was doing something like this:

val longVal = 5;
val doubleVal = Double.parseDouble(longVal.toString());

There must be a simpler way to do this, but what is it?

Try this:

val doubleVal = Double.fromLongBits(longVal)


No go.  Reading around, I feel like this is used to convert a binary string in long format to a IEEE double.

Martha Kim

unread,
Dec 8, 2010, 2:01:17 PM12/8/10
to coms49...@googlegroups.com
2. I'm trying to convert a long to a double.  However, the only way I found was doing something like this:

val longVal = 5;
val doubleVal = Double.parseDouble(longVal.toString());

There must be a simpler way to do this, but what is it?

Try this:

val doubleVal = Double.fromLongBits(longVal)


No go.  Reading around, I feel like this is used to convert a binary string in long format to a IEEE double.

If this doesn't work I'd go w. Karthik's technique, that appears to be the only other technique viable technique that particular piece of documentation mentions.  I looked at the Math library, but round() etc. don't seem to convert types.


Chatura Atapattu

unread,
Dec 8, 2010, 2:02:58 PM12/8/10
to coms49...@googlegroups.com
Thanks you.
Reply all
Reply to author
Forward
0 new messages