Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:31 29 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Engineering format for MMBasic

Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6101
Posted: 05:30pm 15 Mar 2014
Copy link to clipboard 
Print this post

Sometimes it is preferable to format numbers in either exponential or engineering formats
This function does either.
Engineering notation is exponential notation with the exponent alwas a multiple of 3
The function was written for the MicroMite and works but does not display a positive sign for the exponent on other versions of MMbasic.
The three inputs are:
number to format
number of decimal places displayed
engineering notation (anything other than zero = engineering), zero or omitted = standard exponential

' MicroMite
'format number in Engineering notation
' TassyJim 16 March 2014
main:
input "Number to format";n
print str$(n)+" "+StrE$(n, 3)+" "+StrE$(n, 3, 1)
goto main

function strE$(mx,p, eng)
' mx = number to format
' p = number of decinal places
' if eng is not zero, use engineering notation
local px, sn$, x
sn$=" "
x=mx
If x < 0 then
sn$ = "-"
x = x * -1
EndIf
if x<>0 then ' zero is a special case
px=int(Log(x)/ log(10))
if eng <> 0 then ' engineering notation - multiples of 3
px = int(px/3)*3
endif
x=x*(10^-px)
strE$ = sn$ + Str$(x, 0, p) + "e" + Str$(px,-1)
else
strE$ = sn$ + Str$(x, 0, p)
endif
end function


The output:
Number to format? 12345.678
12345.7 1.235e+4 12.346e+3


Jim
VK7JH
MMedit   MMBasic Help
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024