View Full Version : Scientific Notation
peralta_mike
20-04-2011, 20:53
I'm looking for a thin basic function to convert a double float (string format) to scientific notation (string format).
i.e. input: "12456" output: "1.2456E4"
Any suggestions?
Petr Schreiber
20-04-2011, 21:30
Hi,
FORMAT$ should work for you:
Dim SomeNumber As Number = 12456
MsgBox 0, Format$(SomeNumber, "#.######E-#")
Petr
peralta_mike
21-04-2011, 00:07
Thanks Petr.
I will try that.
peralta_mike
22-04-2011, 17:37
Hello,
I don't get any output displayed when I run the
following tb script. The 2nd parameter passed
to my scientific() function is the number of significant
digits after the decimal point.
However, if I display the value of y from within
my scientific() function it displays ok.
Any insights?
'=======================================
uses "console" : uses "os" : uses "file"
dim outtext as string = ""
dim cstr as string
cstr = "78927349432"
outtext=scientific(cstr,3)
console_write outtext & $CRLF
console_waitkey
'========================================
function scientific(cstr as string,d as integer) as string
dim y as string
y=format$(val(cstr),tstr$(d+1)&"|0.000000E+000")
y=trim$(y)
return(y)
end function
'=========================================
Petr Schreiber
22-04-2011, 19:32
Hi,
the problem was in writing
return (y)
instead of
return y
When you do this change, the code works. Also one note - to use multiple modules, you don't have to use separate statements, just separate the module names with comma.
'=======================================
Uses "console", "os", "file"
Dim outtext As String = ""
Dim cstr As String
cstr = "78927349432"
outtext=scientific(cstr,3)
Console_Write outtext & $CRLF
Console_WaitKey
'========================================
Function scientific(cstr As String,d As Integer) As String
Dim y As String
y=Format$(Val(cstr),TStr$(d+1)&"|0.000000E+000")
y=Trim$(y)
Return y
End Function
'=========================================
The use of parenthesis for return is not documented.
Petr
peralta_mike
22-04-2011, 19:42
Thanks petr for the quick reply.
Can return be changed so that return(y) will work also?
return(y) always works for c and perl and I think php
as well. It would be nice and consistent if it were to work
the same way in tb.
- Mike
Petr Schreiber
22-04-2011, 20:07
Hi!,
to make a feature suggestion, you can simply enter Support section and add it here. I already promoted your post to feature suggestion, so you can watch it here :):
http://www.thinbasic.com/community/project.php?issueid=273
Petr
peralta_mike
22-04-2011, 20:45
Thanks again Petr.