Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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.

Edited by JumpyNET
Posted (edited)

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:

   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

Edited by JumpyNET

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...