I mean 8 decimals places without round, I am using round(x-0.00000005),8) but is it the best and fastest way ?
I there no function in vb.net?
Imports System.Runtime.CompilerServices Friend Module DoubleExtensions <Extension> Public Function Truncate(source As Double, decimals As Integer) As Double Dim wholePart = Math.Truncate(source) Dim fractionalPart = source - wholePart fractionalPart = fractionalPart * 10 ^ decimals fractionalPart = Math.Truncate(fractionalPart) fractionalPart = fractionalPart / 10 ^ decimals Return wholePart + fractionalPart End Function End ModuleSample usage:
Dim number = 0.254789457845128 MessageBox.Show(number.ToString()) number = number.Truncate(8) MessageBox.Show(number.ToString())
Sorry, little mistake , it should be : I am using round(x-0.000000005,8)
It is the best I see till now, if I am right truncate is only for type decimal
Sub Main()
Dim mynum As Double = 3.1234567898765
Dim newnum As Double
newnum = Math.Round(mynum, 8) 'Round changes the last digit to a 9 instead of 8
Console.WriteLine("Round to 8: " & newnum) 'not what you want
'Truncate to 8 without rounding:
newnum = Math.Round(mynum, 9) 'or newnum = mynum
newnum = newnum - 0.000000005
newnum = Math.Round(newnum, 8)
Console.WriteLine("Truncate to 8: " & newnum)
Console.ReadLine()
End Sub
Dim mynum As Double = 3.1234567898765
'Truncate to 8 without rounding:
mynum = mynum - 0.000000005
mynum = Math.Round(mynum, 8)
Console.WriteLine("Truncate to 8: " & mynum)
Um, yes you're missing something. It's like you didn't even bother to read post #5. I already said myself that Math.Truncate will not truncate to a specific number of decimal places so it's no great breakthrough that you worked that out. That would also explain why I wrote a method for you that does what you want and uses Math.Truncate internally. I even provided an example of how to use it. If you're not up to copying a bit of code that I went to the trouble of writing for you then I'm wasting my time here.Yes, but it truncates all the decimals and it is so no solution for my question.
Till now the best I think is round(x-0.000000005,8)
Do I miss something, Jimcilhinney ?
Yes, Solitaire the same as what I propose.
And I think it is till now the best.
And not the solution Jimcilhinney which is more complicated and not so fast I think.
Imports System.Runtime.CompilerServices
Friend Module DoubleExtensions
<Extension>
Public Function Truncate(source As Double, decimals As Integer) As Double
Dim wholePart = Math.Truncate(source)
Dim fractionalPart = source - wholePart
fractionalPart = fractionalPart * 10 ^ decimals
fractionalPart = Math.Truncate(fractionalPart)
fractionalPart = fractionalPart / 10 ^ decimals
Return wholePart + fractionalPart
End Function
End Module
Dim source = 0.254789457845128 Dim destination As Double Dim timer As Stopwatch timer = Stopwatch.StartNew() For i = 1 To 100000 destination = Math.Round(source - 0.000000005, 8) Next Dim time1 = timer.Elapsed timer = Stopwatch.StartNew() For i = 1 To 100000 destination = source.Truncate(8) Next Dim time2 = timer.Elapsed MessageBox.Show(String.Format("Round: {1}{0}Truncate: {2}{0}Difference: {3}", Environment.NewLine, time1, time2, time2 - time1))It showed that using Round was about an order of magnitude faster than using Truncate. The thing is though, Calling Truncate 100,000 times still only takes about 5/100 of a second, so it's not a difference that anyone is ever going to notice unless they're doing some serious number crunching.