Post Reply 
(42) Feet inches fractions
07-11-2021, 08:31 PM (This post was last modified: 06-22-2022 06:43 PM by DMaier.)
Post: #1
(42) Feet inches fractions
This code takes decimal inches, e.g., 14.25 in X and returns the following in Alpha:
  • feet inches-fractions if flag 1 is clear, e.g., 1' 2-1/4"
  • inches-fractions if flag 1 is set, e.g., 14-1/4"
The intended application is formatting inches for display or printing after performing decimal calculations.

The maximum denominator, which should be a power of 2, is assumed to be set in variable DEN (store 16 in DEN if you don't want anything smaller than sixteenths, for example). Note that the code rounds to the nearest fraction.

The code uses flag 82 internally and doesn't preserve the stack (but see suggested modification in comments).

EDIT (2022-06-22): Updated to remove a serious bug in handling non-integer values that rounded to integers.

Sample output (DEN=8, flag 1 clear):

Code:

12.0000     1'
12.4900     1' 1/2"
 1.0000     1"
 1.2500     1-1/4"
14.0000     1' 2"
14.0626     1' 2-1/8"
14.3125     1' 2-3/8"
 4.0010     4"
 7.9990     8"

Code:

00 { 138-Byte Prgm }
01▸LBL "FIF" 
02 CLA  @ Free42 (including DM42): insert FUNC 00 above to preserve stack
03 CF 82
04 RCL× "DEN"
05 0.5
06 +
07 IP
08 RCL÷ "DEN"
09 ENTER
10 FS? 01
11 GTO 00
12 IP
13 X=0?
14 GTO 01
15 ENTER
16 12
17 ÷
18 IP
19 X=0?
20 GTO 01
21 AIP
22 ├"' "
23▸LBL 01
24 R↓
25 ENTER
26 ENTER
27 12
28 MOD
29▸LBL 00
30 IP
31 X=0?
32 GTO 02
33 SF 82
34 AIP
35▸LBL 02
36 R↓
37 FP
38 X=0?
39 GTO 05
40 FS? 82
41 ├"-"
42 SF 82
43 RCL× "DEN"
44 STO "N"  @ Use LSTO on Free 42 (including DM42)
45 RCL "DEN"
46 STO "D" @ Use LSTO on Free 42 (including DM42)
47▸LBL 03
48 RCL "N"
49 0
50 BIT?
51 GTO 04
52 2
53 STO÷ "D"
54 STO÷ "N"
55 GTO 03
56▸LBL 04
57 RCL "N"
58 AIP
59 ├"/"
60 RCL "D"
61 AIP
62▸LBL 05
63 FS? 82
64 ├"""
65 AVIEW
66 RTN
67 END
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(42) Feet inches fractions - DMaier - 07-11-2021 08:31 PM
RE: (42) Feet inches fractions - DMaier - 06-22-2022, 07:47 PM



User(s) browsing this thread: 1 Guest(s)