Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Perfect conversion for bytes to kilobytes conversion and back forth  RSS feed

 
Roger Jam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , I have a requirement where i need to convert bytes to kilobytes.
I am doing this by dividing by data/1024.
But when i am again assigning it back by * 1024.I am getting different value.
Please suggest me a perfect way to convert bytes to kb and again reconvert the value back.

thanks in advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger lelu wrote:I am doing this by dividing by data/1024.

What are you storing the result in? Doubles have rounding errors. BigDecimal keeps the entire decimal value. Then when you multiply back, you get the original because no precision was lost.
 
Roger Jam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne . I was using doubles . I will try BigDecimal.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to store it in a double/BigDecimal? The number of bytes isn't a floating point number so it should be stored in an integer/long/BigInteger.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but if you want to convert bytes to kilobytes, you may very well end up with a result that is a decimal, no?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, i wouldn't convert back and forth. Store it one way. When you need the other one, calculate and use it, but keep the original value. Storing the data twice is (almost) always a bad idea. converting back and for and back again is bound to lead to errors.
 
Roger Jam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your suggestions.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, looking at the original post, I think a double has more than enough precision for this problem. I suspect the real problem was in how the division was performed in the first place, before it was stored as a double. Was the calculation performed like this?

Or was it performed like this?

The difference between the two may look minor, but it can make a big difference. (Hint: I am assuming "bytes" is an int or long here.) Can you figure out why it makes a difference?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I also agree with Fred's comments. However, converting back and forth is a useful test to see how accurate the conversion is. If you can't get the right number, you're probably doing the conversion wrong.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!