StockFetcher Forums · Filter Exchange · How to extract specific digits from values << >>Post Follow-up
485 posts
msg #105079
2/21/2012 3:35:58 AM

For example: I want the hundredth digit of the close of SPY on 2/17/2012 = 136.41

This method worked as expected:
a) 136.41 * 100 = 13641.33 ... shift hundredth digit 2 places to left, revealing additional precision
b) mod(13641.33,1) = remainder of (13641.33/1) = 0.33 ... frac() and int() functions would also be useful for these
c) 13641.33 - 0.33 = 13641.00 ... clean up the right side
d) 13641.00 / 10 = 1364.10 ... shift digits 1 place to right
e) mod(1364.10,1) = remainder of (1364.10/1) = 0.10
f) 0.10 x 10 = 1.00 ... shift digit 1 place to left

Fetcher[
/* ===== extract hundredth digit ... with NO rounding of the hundredth digit ===== */
set{a, close * 100}
set{b, mod(a, 1)}
set{c, a - b}
set{d, c / 10}
set{e, mod(d,1)}
set{f, e * 10}

]

*Here is another version that I thought would work, but for some reason this method did not produce the results I expected. In some cases it rounded the hundredth digit up and in other cases it rounded the hundredth digit down.
a) 136.41 * 10 = 1364.133
b) mod(1364.133,1) = remainder of (1364.133/1) = 0.133
c) 0.133 x 10 = 1.33
d) mod(1.33,1) = 0.33
e) 1.33 - 0.33 = 1.00

Fetcher[
/* ===== extract hundredth digit from close ... with rounding of the hundredth digit ===== */
set{a, close * 10}
set{b, mod(a, 1)}
set{c, b * 10}
set{d, mod(c, 1)}
set{e, c - d}