Question double , number of decimal places

andrews

Well-known member
Joined
Nov 22, 2011
Messages
167
Programming Experience
5-10
Is there a fast way to get a number of double type with n decimal places.

f.e. x = 0.254789457845128.......
I want x as 0.25478945 with 8 decimals

Thanks for any response
 
That's true, but what will you do if you want to truncate to other than 8 decimal places? Maybe you don't need flexibility but it's not a bad idea to provide it anyway. It's really a good idea to build up a reusable library of functions like this that you can reference in any project and not have to keep doing the same thing over and over.
 
Yes, you are right but my function TruncateNew is doing this.
And I have already a DLL library with 31 functions and 18 subs.
Regards
 
Last edited:
And a little faster

Public Function TruncateNew(ByVal source As Double, ByVal decimals As Integer) As Double
Dim i As Integer
Dim difference As Double
difference = 2
For i = 1 To decimals
difference = difference * 10
Next
difference = 1 / difference
Return Math.Round(source - difference, decimals)
End Function

time : 11.1 seconds
 
Back
Top