'This project makes a previous project more complete. And that project will
'complete a still earlier project. This source code is also at"
' http://www.yarker-dsyc.info/D_Notebo...fixFormat.html
'along code to demonstrate the function, and more description than I want
'to put here.
'For questions please start a thread in "Programming".
'
'SI prefix format
'Significand formated as 1 to less than 1000, and append prefix for exponents
'that are a multiple of 3.
'
'
'
'complete a still earlier project. This source code is also at"
' http://www.yarker-dsyc.info/D_Notebo...fixFormat.html
'along code to demonstrate the function, and more description than I want
'to put here.
'For questions please start a thread in "Programming".
'
'SI prefix format
'Significand formated as 1 to less than 1000, and append prefix for exponents
'that are a multiple of 3.
'
Code:
'Copyleft 2022 by Dale Yarker; do what you want with this code except claim it 'as yours. #compile sll "SI_PrefixFormatB3.sll" #dim all function SI_Format(byval NumToFormat as ext, _ opt byval DigitsOut as long, _ opt byval DP_Align as long) common as wstring 'local AbsNumToFormat, Inverse10E, as ext local SignificandStr, SI_Prefix as wstring local NumIsNeg, NumIsPos, e_location, E, E_Mod3 as long '------------------------------------------- Format zero and set sign variabes if NumToFormat = 0& then if DP_Align then function = " 0.0" else function = "0.0" end if exit function elseif NumToFormat < 0& then NumIsNeg = 1& else NumIsPos = 1& end if '----------------------------------------------- Check/Set number of DigitsOut if DigitsOut = 0 then 'set default if optional parameter not used DigitsOut = 6& else 'set to min or max if parameter outside limits if DigitsOut < 3& then DigitsOut = 3& elseif DigitsOut > 18& then DigitsOut = 18& end if end if '----------------------------------------------- SignificandStr = format$(NumToFormat, string$(DigitsOut, "#") + "e-##") e_location = instr(SignificandStr, "e") E = val(mid$(SignificandStr, e_location + 1&)) + DigitsOut - 1 SignificandStr = mid$(SignificandStr, 1 to e_location) if E >= 0& then E_Mod3 = E mod 3& E \= 3& else E += -2& E_Mod3 = E mod 3& + -1& E \= 3& end if '============================= '------------------ Use modulus to place decimal point, and space to align. -- if E_Mod3 = 2 then ' · · · · · · · · · · · · · · · · · · · · · is hundreds · · if DigitsOut > 3 then SignificandStr = strinsert$(SignificandStr, ".", 4 + NumIsNeg) end if if DP_Align and NumIsPos then SignificandStr = strinsert$(SignificandStr, " "$$, 1) end if elseif E_Mod3 = 1 then ' · · · · · · · · · · · · · · · · · · · · · is tens · · SignificandStr = strinsert$(SignificandStr, ".", 3 + NumIsNeg) if DP_Align then SignificandStr = strinsert$(SignificandStr, space$(1 + NumIsPos), 1) end if elseif E_Mod3 = 0 then ' · · · · · · · · · · · · · · · · · · · · is units · · SignificandStr = strinsert$(SignificandStr, ".", 2 + NumIsNeg) if DP_Align then SignificandStr = strinsert$(SignificandStr, space$(2 + NumIsPos), 1) end if elseif E_Mod3 = -1 then '· · · · · · · · · · · · · · · · · is hundreds of · · if DigitsOut > 3 then SignificandStr = strinsert$(SignificandStr, ".", 4 + NumIsNeg) end if if DP_Align and NumIsPos then SignificandStr = strinsert$(SignificandStr, " "$$, 1) end if elseif E_Mod3 = -2 then '· · · · · · · · · · · · · · · · · · · is tens of · · SignificandStr = strinsert$(SignificandStr, ".", 3 + NumIsNeg) if DP_Align then SignificandStr = strinsert$(SignificandStr, space$(1 + NumIsPos), 1) end if else '· · · · · · · · · · · · · · · · · · ·· · · · · · · · · · is units of · · SignificandStr = strinsert$(SignificandStr, ".", 2 + NumIsNeg) if DP_Align then SignificandStr = strinsert$(SignificandStr, space$(2 -+ NumIsPos), 1) end if end if '. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . '----------------------- Use exponent divided by 3 to select prefix letter. -- if abs(NumToFormat) >= 1& then '·························· AbsNumtoFormat >= 1 if E > 3& then '························································ E>3 if E > 7& then '······················································ E>7 if E >= 9& then '·················································· E>=9 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Error ++ goto OutOfRange else '······························································ E<9 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e24«»e26 ++ SI_Prefix = "Y"$$ end if ' elseif E < 7& then '·················································· E<7 if E > 5& then '···················································· E>5 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e18«»e20 ++ SI_Prefix = "E"$$ elseif E < 5 then '··················································E<5 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e12«»e14 ++ SI_Prefix = "T"$$ else '······························································ E=5 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e15«»e17 ++ SI_Prefix = "P"$$ end if else '································································ E=7 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e21«»e23 ++ SI_Prefix = "Z"$$ end if elseif E < 3& then '···················································· E<3 if E > 1& then '·······················································E>1 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e6«»e8 ++ SI_Prefix = "M"$$ elseif E < 1& then '·················································· E<1 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e0«»e2 ++ SI_Prefix = ""$$ SignificandStr = rtrim$(SignificandStr, "e") else '································································ E=1 SI_Prefix = "k"$$ end if 'E1 else '·································································· E=3 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9«»11 ++ SI_Prefix = "G"$$ end if 'E3 else '·····················································(fractions)·· NTF<1 if E < -4& then '······················································ E<-4 if E < -8& then '···················································· E<-8 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Error ++ goto OutOfRange elseif E > -8& then '················································ E>-8 if E < -6& then '·················································· E<-6 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-19«»e-21 ++ SI_Prefix = "z"$$ elseif E > -6& then '·············································· E>-6 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-13«»e-15 ++ SI_Prefix = "f"$$ else '····························································· E=-6 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-16«»e-18 ++ SI_Prefix = "a"$$ end if 'E-6 else '······························································· E=-8 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-22«»e-24 ++ SI_Prefix = "y"$$ end if 'E-8 elseif E > -4& then '·················································· E>-4 if E < -2& then '···················································· E<-2 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-7«»e-9 ++ SI_Prefix = "n"$$ elseif E > -2& then ' '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-1«»e-3 ++ SI_Prefix = "m"$$ else '······························································· E=-2 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-4«»e-6 ++ SI_Prefix = "µ"$$ end if 'E-2 else '································································· E=-4 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ e-10«»e-12 ++ SI_Prefix = "p"$$ end if 'E-4 end if 'NTF1 ' mid$(SignificandStr, len(SignificandStr), 1&) = $$spc function = SignificandStr + SI_Prefix' ' exit function '=============================================================== OutOfRange: function = "Out of SI range."$$ end function '
'
Comment