Double scientific notation to decimal notation in java?

It sounds like you are reading the value wrong from the client, but in any case, NumberFormat is your friend :) java.sun.com/j2se/1.5.0/docs/api/index.h....

It sounds like you are reading the value wrong from the client, but in any case, NumberFormat is your friend :) java.sun.com/j2se/1.5.0/docs/api/index.h... EDIT: considering the code sample that you posted, I have to agree with @trashgod that your conversion code is flawed. Perhap DataInputStream can assist -> java.sun.com/javase/6/docs/api/java/io/D....

1 The order() method of java.nio. ByteBuffer can be beneficial in this context, too. Java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html – trashgod Feb 4 '10 at 22:38.

Your conversion is flawed in several ways, including the use of signed arithmetic and incorrect bit values. You might study the format and look at the approach shown in this glossary entry.

Thanks trashgod. Your link was realy helpfull for me. I solved the problem.

I will write my solution to here. – Aykut Feb 6 '10 at 11:23.

Your use of the term "scientific notation" sorta suggests you are dealing with text data that looks like "3.0e-1". But I think I understand. It seems like the problem is just reading binary data written in a non-java word order.

Yet why are the integers written big-endian, yet the doubles written little-endian? And why are you reading in such a strange manner? Doesn't your compiler complain about the 'int'?

It may have been hiding a problem. (EDIT: my error - I was stuck on 64 bits for the double) It would be useful for everyone to see a hex dump of your data. Are the bytes flipped, or just the words?

Maybe this code will provide some inspiration. Please excuse the 'get-r-done' use of variables. // convert CLI argument to double, write to file, and read back // without args, default is "0.3" // should try negative numbers!

Import java.io. *; public class BinaryFile { public static void main(String args) { String strFilePath = "WordFlippedDouble"; boolean WRITEOP = true; double d, dd = 0.3; long ddFlip; if(args. Length > 0) { dd = Double.

ValueOf(args0); } System.out. Println("Starting with " + dd + " looks like " + Long. ToHexString(Double.

DoubleToLongBits(dd))); if(WRITEOP) { ddFlip = Double. DoubleToLongBits(dd); ddFlip = (ddFlip>32) & 0xFFFFFFFFL); System.out. Println("WRITE: (flipped) looks like " + Long.

ToHexString(ddFlip)); try { FileOutputStream fout = new FileOutputStream(strFilePath); DataOutputStream dout = new DataOutputStream(fout); dout. WriteLong(ddFlip); dout.close(); } catch (Exception e) { System.out. Println("ERROR: " + e.getMessage()); } } if(false) return; // testing try { FileInputStream fin = new FileInputStream(strFilePath); DataInputStream din = new DataInputStream(fin); ddFlip = din.readLong(); d = Double.

LongBitsToDouble((ddFlip>32) & 0xFFFFFFFFL)); System.out. Println("READ: " + Long. ToHexString(ddFlip) + " converts to " + d + " DIFF: " + (dd-d)); din.close(); } catch(FileNotFoundException e) { System.out.

Println("FileNotFoundException : " + e); } catch(IOException e) { System.out. Println("IOException : " + e); } } }.

Hmmm, I made an interesting slip-up, and duplicated it. It just happened not to make a difference due to the rules for promotion... – gary Feb 5 '10 at 3:37 let me explain why I have to do something like this. We are coding on solaris sparc os in c++ for server and client side of our program.

But the client side has been coded with java in last mounths. Then we got a new request about supporting x86(including linux based and solaris x86 OSs) platforms. Then I started to port server side from solaris to linux and solaris sparc to solaris x86.

When server side running on x86 machines we encuntered the endian issues. So, server side has 500000 lines of code and we decided to convert numerical datas according to system architecture on client(java) side. – Aykut Feb 6 '10 at 11:18 1 Your original question stated you were sending the data without conversion.

Are all your servers sending the same format? A (short) data dump, with hex and numeric value, would help a lot. I can see no way to send 0.3 (0x3fd3333333333333) and get 9.534475227E-315 (0x0000000073066666) without some serious mix-up.

I believe my program should help show the method of conversion, once you know what your are converting. I cleaned it up to remove some confusion. – gary Feb 6 '10 at 17:13.

Don't roll your own bit twiddling like that. It's in the standard API. See java.nio.ByteBuffer.

Protected int readInt(InputStream stream) throws IOException { ByteBuffer buffer = ByteBuffer. Allocate(Integer. SIZE / Byte.

SIZE); //buffer. Order(ByteOrder. LITTLE_ENDIAN); // optional stream.

Read(buffer.array()); return buffer. GetInt(0); } protected double readDouble(InputStream stream) throws IOException { ByteBuffer buffer = ByteBuffer. Allocate(Double.

SIZE / Byte. SIZE); //buffer. Order(ByteOrder.

LITTLE_ENDIAN); // optional stream. Read(buffer.array()); return buffer. GetDouble(0); }.

If you sent 0.3 from server to client and got back 9.534475227E-315 the last thing you ought to do is convert that value to 0.3 again. The returned value is very close to 0 in f-p terms, and indicates some error in the sending and returning process. I am puzzled at your question, I did not know that Java implemented a Decimal class but then my Java knowledge is old and scant.Do you perhaps mean a BigDecimal?

Or are you converting string formats of floating-point numbers, which would be a completely different kettle of fish? Regards Mark.

I solved the my question and thank to you all for your helps. Much appreciated. So here is the solution.

Protected double readDouble(InputStream stream) throws IOException { return Double. LongBitsToDouble(((long) read(stream) & 0xff) | ((long) (read(stream) & 0xff).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions