wood burning stoves 2.0*
The moose likes Java in General and the fly likes Double parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Double parsing" Watch "Double parsing" New topic
Author

Double parsing

Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
I have a fixed width string of 8 bytes. The last two bytes indicate the frational part how do i parse it to double

example 00001223 is actaully 12.23
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1. Take a substring of your string, with the first 6 characters
2. Append a "."
3. Append the last 2 chracters
4. use Double(String) constructor to make a new Double

I'm sure you can make the code yourself


[My Blog]
All roads lead to JavaRanch
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello Amit,


The last two bytes indicate the frational part how do i parse it to double


There is a method named doubleValue() of java.lang.Byte Wrapper class which returns the double representation of the byte primitive value it has.

If you need to have >1 bits, then you may need to a bit manipulation with logical ANDing. And then use the Double(String) constructor for getting the double value in turn.

HtH.
[ June 20, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
Thank you ..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Even simpler:

1. Parse the string using Double.parseDouble
2. Divide the result by 100.0
[ June 21, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Jesper's solutions looks very simple! You can go for that.

Thanks Jesper!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double parsing
 
Similar Threads
object Memory
Eclipse - Incompatible minimum and maximum heap sizes specified -screen shot attached
io
asking question regarding string memory allocation in array
ZipOutputStream remaining bits