• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Perfect conversion for bytes to kilobytes conversion and back forth

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne . I was using doubles . I will try BigDecimal.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your suggestions.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
reply
    Bookmark Topic Watch Topic
  • New Topic