JumpyNET Posted March 6, 2008 Posted March 6, 2008 (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 March 6, 2008 by JumpyNET Quote
Administrators PlausiblyDamp Posted March 6, 2008 Administrators Posted March 6, 2008 Second one first... 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... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
JumpyNET Posted March 10, 2008 Author Posted March 10, 2008 (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 March 10, 2008 by JumpyNET Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.