VBA Get Function

Boingts

New Member
Joined
Mar 14, 2019
Messages
6
Hi all

I have a scenario where I need to truncate a number to 4 decimal places ( no rounding) .

I have searched google and this site and found the Get() Function. This function works great IF the decimals to 4 places are greater 0 ( 515.1234678 gives me 515.1234) however if the number is to only 1, 2 or 3 decimal places places it does not work ( 515.40000000 gives me 515.3999 - my expectation would be 515.40000000 = 515.4000)

Here are the two Functions i have used to try get this resolved
Code:
Function FixFuntion(number As Double) As Double
FixFuntion = Fix(number * 10000) / 10000
End Function


Function FixDecimal(ByVal number As Double, ByVal digits As Integer) As Double
    Dim x As Integer
    x = 10 ^ digits
    FixDecimal = Fix(number * x) / x
End Function
Am I using these correctly or is there another fix that could be used ?

Thank you in advance
 
Last edited by a moderator:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,351
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Welcome to the forum.

I can't replicate that. How are you using the function - is the value you are passing in the result of a formula, or a typed-in value?
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,793
Office Version
365
Platform
Windows
Doesnt the built in TRUNC function do just what you want or am i missing something?
 

Boingts

New Member
Joined
Mar 14, 2019
Messages
6
Welcome to the forum.

I can't replicate that. How are you using the function - is the value you are passing in the result of a formula, or a typed-in value?
Hi Rory - thank you the reply

I have the FixFuntion (contains the Get) wrapped around a formula, however if i just use the number typed into a cell i get the same result.

The primary one of the examples i am using is
Code:
Function FixFuntion(number As Double) As Double
FixFuntion = Fix(number * 10000) / 10000
End Function
Here are series of examples i used the function with

500.40000000 = 500.3999 - unexpected - expected 500.4000
500.41000000 = 500.4100 - expected
500.41200000 = 500.4119 - expected
500.41230000 = 500.4123 - expected
500.41234000 = 500.4123 - expected
500.41234500 = 500.4123 - expected
500.41234560 = 500.4123 - expected
500.4123457 = 500.4123 - expected

The Hack of the fix function was something i found on Google and may be the issue

If I try anything else it seems to round the figure which is not what i require

Cheers
 

Boingts

New Member
Joined
Mar 14, 2019
Messages
6
Hi Steve - I am using the in VBA and not Excel ( Trunc works great in Excel) - I have tried to use the With Application in VBA but Trunc is not on the list
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,793
Office Version
365
Platform
Windows
I can replicate what you are saying and the maths you use seems sound. It's the computation within vba that seems to go wrong. I can get the 515.4 to work but then others produce incorrect results. I'll see if I can see a pattern. Clearly if 515.4 is passed to the function it shouldn't produce 515.3999 based on what is being asked of it.
 

Boingts

New Member
Joined
Mar 14, 2019
Messages
6
I can replicate what you are saying and the maths you use seems sound. It's the computation within vba that seems to go wrong. I can get the 515.4 to work but then others produce incorrect results. I'll see if I can see a pattern. Clearly if 515.4 is passed to the function it shouldn't produce 515.3999 based on what is being asked of it.
Hi Steve, thanks for looking at this, and i am glad you agree as i thought i was going crazy. Do you know of any other way of replicating the Excel Trunc built in function in VBA ?

Cheers
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,793
Office Version
365
Platform
Windows
See if you can find one that doesnt work when the calc is split up.

Code:
Function FixFunction(number As Double) As Double

number = number * 10000
number = Fix(number)
number = number / 10000
FixFunction = number

'FixFunction = Fix(number * 10000) / 10000

End Function

Sub test()

Dim x As Double

x = 515.4

MsgBox FixFunction(x)

End Sub
 

Boingts

New Member
Joined
Mar 14, 2019
Messages
6
See if you can find one that doesnt work when the calc is split up.

Code:
Function FixFunction(number As Double) As Double

number = number * 10000
number = Fix(number)
number = number / 10000
FixFunction = number

'FixFunction = Fix(number * 10000) / 10000

End Function

Sub test()

Dim x As Double

x = 515.4

MsgBox FixFunction(x)

End Sub
Hi Steve - I ran that but all answers = 0, Also the Msg Box displayed 0 too

Cheers
 

Forum statistics

Threads
1,082,360
Messages
5,364,920
Members
400,815
Latest member
Joaquin Phoenix

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top