0.000001403012501 --> 1.40301e-06

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
1)
How can I round a variable of the type Double to six most significant numbers (not six decimals)?
0.000001403012501 --> 0.00000140301
1403012501 --> 1403010000

2)
How can I convert a variable of the type Double to string in exponential form - even if the exponent would be something very small?
0.00000140301 --> "1.40301e-06"

3)
I bet you could do the thing in the first question at same time you do the thing in the second question, but I want to keep those procedures separate.
 
Last edited:
Second one first...
Visual Basic:
Dim d As Double = 0.00000140301
MessageBox.Show(d.ToString("E3"))
MessageBox.Show(d.ToString("E4"))
MessageBox.Show(d.ToString("E5"))
will have to look into the other one...
 
Thank you PlausiblyDamp for helping me with the exponent question.

I solved the rounding to significant figures myself. I just love programming because there is so much math involved. :D

Here is the solution:
Code:
    Function SigRnd(ByVal d As Double, ByVal n As Integer) As Double
        Dim RetVal As Double = 0

        If d <> 0 Then
            Dim FirsSignicantPos As Integer = Math.Floor(Math.Log10(Math.Abs(d)))
            Dim RoundingPos As Integer = FirsSignicantPos - (n - 1)
            Dim NonSignificantPart As Double = d Mod (10 ^ RoundingPos)
            RetVal = (d - NonSignificantPart)
        End If

        Return RetVal
    End Function
 
Last edited:
Back
Top