Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to display a float number in scientific?
#1
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
Reply
#2
For example:
Code: (Select All)

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

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

What do those "^"'s do?
b = b + ...
Reply
#4
(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
Shoot first and shoot people who ask questions, later.
Reply
#5
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
Reply
#6
(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...36#pid5236

Pete
Shoot first and shoot people who ask questions, later.
Reply
#7
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]
Reply
#8
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
Reply
#9
@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()
Reply
#10
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
Shoot first and shoot people who ask questions, later.
Reply




Users browsing this thread: 6 Guest(s)