Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active January 5, 2021 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgchris/4fed348af1782340e27b8834b87b4e67 to your computer and use it in GitHub Desktop.
Save rgchris/4fed348af1782340e27b8834b87b4e67 to your computer and use it in GitHub Desktop.
Form Decimal
Rebol [
Title: "Form Decimal for Rebol 2"
Date: 4-Jan-2021
Author: "Christopher Ross-Gill"
Purpose: "Render a decimal! value sans scientific notation"
Tests: [
"12345000000000000000000"
"0.0000000000001"
"0.1"
"-0.1"
"0.0000000000004"
"0.0000000000005"
"0.0000000000009"
"0.0000099"
"0.0009999"
"0.0005001"
"0.0004999"
"0.00002"
"0.0021"
"2000000000000000"
"-9900000000000000"
"1.4142135623731"
"3.14159265358979"
"987.654"
"-987.654"
"-987.654"
"1111110.11111111"
"9999990.99999999" "11" "111" "999" "-11" "-111"
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
"-1" "-2" "-3" "-4" "-5" "-6" "-7" "-8" "-9"
"5550" "1110" "1000" "9999" "99.99" "-9999" "-9900" "-5550"
"12345.6789" "98765.4321"
]
]
; not a perfect shim for Ren-C's ELIDE--returns UNSET! in the case of (ELIDE VALUE)
elide: func [
"First argument is evaluative, but discarded"
discarded [any-type!]
value [any-type! unset!]
][
get/any 'value
]
form-decimal: func [
"Render a decimal! value sans scientific notation"
value [integer! decimal!]
/local text sign float whole part
][
case [
integer? value [
text: form value
]
zero? value [
text: copy "0"
]
elide (
text: tail copy either negative? value [
value: abs value
"-"
][
""
]
float: to-integer log-10 value
value: form multiply power 10 negate float value
whole: form first value
part: remove/part value 2
)
zero? float [
insert text whole
any [part = "0" insert insert tail text "." part]
]
negative? float [
insert insert/dup insert text "0." #"0" -1 - float whole
any [part = "0" insert tail text part]
]
float < length? part [
insert text part
insert skip text float #"."
any [whole = "0" insert text whole]
]
'else [
insert/dup insert text part #"0" float - length? part
any [whole = "0" insert text whole]
]
]
head text
]
Rebol [
Title: "Form Decimal for Ren-C"
Date: 4-Jan-2021
Author: "Christopher Ross-Gill"
Purpose: "Render a decimal! value sans scientific notation"
Tests: [
"0.000000054321654321"
"0.00000005"
"420000000000000000"
"0.000045678"
"1.1"
"-0.000045678"
"12345000000000000000000"
"0.0000000000001"
"0.1"
"-0.1"
"0.0000000000004"
"0.0000000000005"
"0.0000000000009"
"0.0000099"
"0.0009999"
"0.0005001"
"0.0004999"
"0.00002"
"0.0021"
"2000000000000000"
"-9900000000000000"
"1.4142135623731"
"3.14159265358979"
"987.654"
"-987.654"
"-987.654"
"1111110.11111111"
"9999990.99999999" "11" "111" "999" "-11" "-111"
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
"-1" "-2" "-3" "-4" "-5" "-6" "-7" "-8" "-9" "-10"
"5550" "1110" "1000" "9999" "99.99" "-9999" "-9900" "-5550"
"12345.6789" "98765.4321"
]
]
digit: charset "0123456789"
onenine: charset "123456789"
form-decimal: func [
"Render a decimal! value sans scientific notation"
value [integer! decimal!]
<local> sign whole part exp
][
part: _
exp: _
parse form value [
copy sign opt "-"
copy whole [onenine any digit | #"0"]
opt ["." copy part [any #"0" onenine any digit] | ".0"]
opt [#"e" copy exp [opt "-" some digit]]
end
] else [
fail ["Could not parse:" mold form value]
]
unspaced either exp [
either negative? exp: to integer! exp [
["0." head insert/dup copy "" #"0" -1 + abs exp whole part]
][
[whole part head insert/dup copy "" #"0" subtract abs exp length of any [part ""]]
]
][
[sign whole if part ["."] part]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment