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