QB64 Phoenix Edition
How to display a float number in scientific? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: How to display a float number in scientific? (/showthread.php?tid=2903)

Pages: 1 2


How to display a float number in scientific? - desA - 08-04-2024

Good evening everyone,

I would like display a float number in scientific mode to a set number of decimal places - for display printout purposes. 

E.g.  12345.6 -> 1.23E4

Please advise the appropriate command/s to do this in qb64pe. 

Many thanks...  Smile


RE: How to display a float number in scientific? - Kernelpanic - 08-04-2024

For example:
Code: (Select All)

Print Using "##.##^^^^"; 12345.6

Print Using


RE: How to display a float number in scientific? - bplus - 08-04-2024

I am curious
Code: (Select All)
Print Using "##.##^^^^"; 12345.6789123

What do those "^"'s do?


RE: How to display a float number in scientific? - Pete - 08-04-2024

(08-04-2024, 02:37 PM)bplus Wrote: I am curious
Code: (Select All)
Print Using "##.##^^^^"; 12345.6789123

What do those "^"'s do?
They are tin foil hats to keep aliens from viewing your output!

Actually: ^^^^ tells PRINT USING to print the answer in exponential format.

Pete


RE: How to display a float number in scientific? - Kernelpanic - 08-04-2024

As Pete has already written. Five ^ results in a three-digit exponent output - but then it's over. Sex is no longer acceptable!  Tongue

Code: (Select All)

Dim As Double a

a = 12345.6
Print Using "##.##^^^^"; a
Print Using "##.##^^^^"; 12345.6

Print Using "##.##^^^^^"; 12345.6



RE: How to display a float number in scientific? - Pete - 08-04-2024

(08-04-2024, 03:03 PM)Kernelpanic Wrote: As Pete has already written. Five ^ results in a three-digit exponent output - but then it's over. Sex is no longer acceptable!  Tongue

Code: (Select All)

Dim As Double a

a = 12345.6
Print Using "##.##^^^^"; a
Print Using "##.##^^^^"; 12345.6

Print Using "##.##^^^^^"; 12345.6
That's why I avoid PRINT USING, so I don't miss out on 'sex'.

I'm not sure what my latest string math post was, but I think this post included my way to convert to SI without using PRINT USING.

https://qb64phoenix.com/forum/showthread.php?tid=756&pid=5236#pid5236

Pete


RE: How to display a float number in scientific? - Kernelpanic - 08-04-2024

Quote:I'm not sure what my latest string math post was, but I think this post included my way to convert to SI without using PRINT USING.
Yes, that's right! It works, programming-wise is it great, but what effort is involved . . .?

[Image: Expo-Darstellung2024-08-04.jpg]


RE: How to display a float number in scientific? - Jack - 08-04-2024

Code: (Select All)

Declare Library
    Function snprintf& (Dest As String, Byval l As Long, frmt As String, Byval x As _Float)
End Declare

Dim As String * 20 result
Dim As String format
Dim As Long resultlen, flag
Dim As Single x

resultlen = Len(result) - 1
result = Space$(resultlen) + Chr$(0)
format = "%.2e" + Chr$(0)
x = 12345.6
flag = snprintf&(result, resultlen, format, x)
Print result

result = Space$(resultlen) + Chr$(0)
format = "%12.2e" + Chr$(0)
x = 12345.6
flag = snprintf&(result, resultlen, format, x)
Print result



RE: How to display a float number in scientific? - Kernelpanic - 08-04-2024

@Jack - It works and is not as complicated as Pete's solution. - A saying comes to mind:

Why do it simply when one can do it complicated?   Tongue

Code: (Select All)

'Umrechnung Dezimalzahl in Exponentialschreibweise - 4. Aug. 2024
'Referenz S.263 - Print Using Formatierungssymbole

Option _Explicit

'Bei Double erscheint ein "D" statt "E"
Dim As Single x, z

Locate 2, 3
Print "Dezimalzahl in Exponentialdarstellung"
Locate 3, 3
Print "====================================="

Locate 5, 3
Input "Dezimalzahl        : ", x

Locate 6, 3
Print Using "Als Exponentialzahl: ##.##^^^^"; x

Locate 8, 3
Input "Weitere Dezimalzahl: ", z

Locate 9, 3
'5 Exponentialsymbole ^ bewirken dreistellige Ausgabe des Exponenten
Print Using "Als Exponentialzahl: ##.##^^^^^"; z

End

PS: I didn't find snprintf() in my C books, only here: snprintf()


RE: How to display a float number in scientific? - Pete - 08-04-2024

Well guys, the whole idea behind BASIC is to figure out how to code it from scratch. PRINT USING relies on a builtin library, and Jack's neat example is a C library; so you really cannot ascertain matters of complexity when you are not really looking under the hood.

Pete