You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RKUtil.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.util;
  16. /**
  17. * Utility class for helping convert RK numbers.
  18. *
  19. * @see org.apache.poi.hssf.record.MulRKRecord
  20. * @see org.apache.poi.hssf.record.RKRecord
  21. */
  22. public final class RKUtil {
  23. private RKUtil() {
  24. // no instances of this class
  25. }
  26. /**
  27. * Do the dirty work of decoding; made a private static method to
  28. * facilitate testing the algorithm
  29. */
  30. public static double decodeNumber(int number) {
  31. long raw_number = number;
  32. // mask off the two low-order bits, 'cause they're not part of
  33. // the number
  34. raw_number = raw_number >> 2;
  35. double rvalue = 0;
  36. if ((number & 0x02) == 0x02)
  37. {
  38. // ok, it's just a plain ol' int; we can handle this
  39. // trivially by casting
  40. rvalue = raw_number;
  41. }
  42. else
  43. {
  44. // also trivial, but not as obvious ... left shift the
  45. // bits high and use that clever static method in Double
  46. // to convert the resulting bit image to a double
  47. rvalue = Double.longBitsToDouble(raw_number << 34);
  48. }
  49. if ((number & 0x01) == 0x01)
  50. {
  51. // low-order bit says divide by 100, and so we do. Why?
  52. // 'cause that's what the algorithm says. Can't fight city
  53. // hall, especially if it's the city of Redmond
  54. rvalue /= 100;
  55. }
  56. return rvalue;
  57. }
  58. }