Division Caculation
For my first coding try, included the line R: Q / 100.00 where Q is predefined as a decimal number. When Q is 10.0 of greater, R is displayed correctly as a decimal. When less that 10.0, it is also displayed with the correct value but in scientific notation. I could find fix for the situation.
posted by: Wayne Brodnax 17-Aug-2010/14:48:05-7:00
There's a format.r function that you may want to look at: http://www.mail-archive.com/list@rebol.com/msg04075.html Here's a little trick you can use to display a nicely formatted decimal, rounded to hundreths: remove form to-money 9 / 100
posted by: Nick 17-Aug-2010/15:49:31-7:00
This is an annoying problem that occurs under Windows and (generally) not under other operating systems.....One of the few glitches in REBOL's excellent cross-platformedness. More discussion here: http://www.rebol.org/ml-display-thread.r?m=rmlFMNC
posted by: Sunanda 19-Aug-2010/3:35:37-7:00
I wrote this function to prevent that annoying scientific notation: f: func [d /local result num fra ex sgn numeric dot] [ numeric: charset [#"0" - #"9"] dot: #"." result: copy "" parse form d [ copy num some numeric opt [dot copy fra some numeric] #"E" copy sgn [#"+" | #"-"] copy ex some numeric ( ;print [num fra ex sgn] result: copy num if fra [append result fra] either sgn = "-" [ insert next head insert/dup result #"0" to-integer ex #"." ] [ ex: either fra [(to-integer ex) - length? fra] [to-integer ex] insert/dup tail result #"0" ex ] ) | some numeric dot some numeric (result: copy form d) | some numeric (result: copy form d) ] either empty? result [d] [head result] ] ;tests print [ 167 = to-decimal probe f 167 167.2 = to-decimal probe f 167.2 0.2 = to-decimal probe f 0.2 0.002 = to-decimal probe f 0.002 3.002 = to-decimal probe f 3.002 34E-5 = to-decimal probe f 34E-5 34E+5 = to-decimal probe f 34E+5 2.34E+5 = to-decimal probe f 2.34E+5 167.234E-5 = to-decimal probe f 167.234E-5 167.234E+5 = to-decimal probe f 167.234E+5 167.234E-25 = to-decimal probe f 167.234E-25 167.234E+25 = to-decimal probe f 167.234E+25 ]
posted by: Endo 20-Aug-2010/5:04:46-7:00
And also look at http://www.compkarori.com/vanilla/display/form-decimal.r
posted by: Endo 20-Aug-2010/5:43:14-7:00
Oh I forgot negative numbers, here is the updated version works well with negative numbers: f: func [d /local result num fra ex sgn numeric dot] [ numeric: charset [#"0" - #"9"] dot: #"." result: copy form d parse form d [ opt #"-" copy num some numeric opt [dot copy fra some numeric] #"E" copy sgn [#"+" | #"-"] copy ex some numeric ( result: copy num if fra [append result fra] either sgn = "-" [ insert next head insert/dup result #"0" to-integer ex #"." ] [ ex: either fra [(to-integer ex) - length? fra] [to-integer ex] insert/dup tail result #"0" ex ] if negative? d [insert result #"-"] ) ] result ]
posted by: Endo 20-Aug-2010/5:53:32-7:00
And the tests: ;tests print [ 167 = to-decimal probe f 167 167.2 = to-decimal probe f 167.2 0.2 = to-decimal probe f 0.2 0.002 = to-decimal probe f 0.002 3.002 = to-decimal probe f 3.002 34E-5 = to-decimal probe f 34E-5 34E+5 = to-decimal probe f 34E+5 2.34E+5 = to-decimal probe f 2.34E+5 167.234E-5 = to-decimal probe f 167.234E-5 167.234E+5 = to-decimal probe f 167.234E+5 167.234E-25 = to-decimal probe f 167.234E-25 167.234E+25 = to-decimal probe f 167.234E+25 newline ;negatives -167 = to-decimal probe f -167 -167.2 = to-decimal probe f -167.2 -0.2 = to-decimal probe f -0.2 -0.002 = to-decimal probe f -0.002 -3.002 = to-decimal probe f -3.002 -34E-5 = to-decimal probe f -34E-5 -34E+5 = to-decimal probe f -34E+5 -2.34E+5 = to-decimal probe f -2.34E+5 -167.234E-5 = to-decimal probe f -167.234E-5 -167.234E+5 = to-decimal probe f -167.234E+5 -167.234E-25 = to-decimal probe f -167.234E-25 -167.234E+25 = to-decimal probe f -167.234E+25 ]
posted by: Endo 20-Aug-2010/5:55:14-7:00
I just wrote this simple function (not tested much, but seems to work fine): format-decimal: func [x /local q] [ either find form x "E-" [ insert/dup (head replace (first q: parse form x "E-") "." "") "0" ((to-integer q/3) - 1) insert head q/1 "0." q/1 ] [form x] ] examples: format-decimal 1 / 233 format-decimal 1 / 4 format-decimal 5 * 7 format-decimal 1 / 83723923452346546467 exp2dec also works nicely http://www.rebol.org/ml-display-thread.r?m=rmlZFPQ
posted by: Nick 20-Aug-2010/11:44:11-7:00
1 / to-decimal format-decimal 1 / pi
posted by: Nick 20-Aug-2010/11:47:46-7:00
This one-liner does precision up to 9 digits: remove/part form to-time (1 / 233) 6
posted by: Nick 20-Aug-2010/14:14:36-7:00
Good but we should do some check before use: remove/part form to-time (2341 / 233) 6 == "0.0472103" >> 2341 / 233 == 10.0472103004292
posted by: Endo 23-Aug-2010/2:51:30-7:00
Ooh yes, that's just intended for fractional amounts between 0 and 1.
posted by: Nick 23-Aug-2010/15:18:30-7:00
So this is better I guess: >> remove find/last form to-time (13341 / 233) ":" == "57.257510729" fractional amounts should between 0 and 59. And we need to check the sign of course.
posted by: Endo 24-Aug-2010/7:07:11-7:00
Yes, that's better :)
posted by: Nick 25-Aug-2010/20:33:06-7:00
Oh there is a hidden trap: >> probe t: find/last/tail form to-time (13341 / 233) ":" == "57.257510729" That is ok. But, >> head t == "0:00:57.257510729" So we should copy it, >> t: copy find/last/tail form to-time (13341 / 233) ":" >> head t == "57.257510729"
posted by: Endo 26-Aug-2010/3:01:08-7:00
|