Jump to content
Sign in to follow this  
Mat

Scientific form conversion

Recommended Posts

Mat

Simple functions, could be useful. need i say more?

#include-once

; #FUNCTION# ===============================================================
;
; Name...........: _ToSci
; Description ...: Converts a floating point number to a scientific one.
; Syntax.........: _ToSci ($fNum)
; Parameters ....: $fNum - the floating point number to convert.
; Return values .: The sci value of the floating point number
; Author ........: MDiesel
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; TBA
; Example .......; MsgBox (48, "Example 1", "2008 in standard form = " & _ToSci (2008))
;
; ===========================================================================

Func _ToSci ($fNum)
   Local $sOp = "/10", $i = 0
   If $fNum < 1 Then $sOp = "*10"
   While $fNum > 10 Or $fNum < 1
      $i += 1
      $fNum = Execute ($fNum & $sOp)
   WEnd
   If $i = 0 Then Return $fNum
   If StringLeft ($sOp, 1) = "*" Then Return $fNum & "e-" & $i
   Return $fNum & "e" & $i
EndFunc ; ==> _ToSci

; #FUNCTION# ============================================
;
; Name...........: _FromSci
; Description ...: Converts a scientific number to a standard one.
; Syntax.........: _FromSci ($sSci)
; Parameters ....: $sSci - The scientific value to convert, eg 2.009e3
; Return values .: The true value of the inputed number
; Author ........: MDiesel
; Modified.......:
; Remarks .......: If you enter a non standard form number, it will simply execute it!
; Related .......:
; Link ..........; TBA
; Example .......; MsgBox (48, "Example 2", "2.008e3 = " & _FromSci ("2.008e3"))
;
; ==========================================================================

Func _FromSci ($sSci)
   Return Execute (StringRegExpReplace ($sSci, "([0-9;.]*)e([0-9]*)", "\(\1*10^\2\)"))
EndFunc ; ==> _FromSci

MDiesel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.