Using VBA - Code To Truncate Equation To One Decimal Place

JJVRANA

New Member
Joined
Jul 30, 2012
Messages
4
Need VBA code to convert for the following Excel worksheet equation:

=Trunc((D26/B26)*0.96),1)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Welcome to the Board!

If you have some value or calculation "x" that you want to Truncate in VBA to one decimal, you can do so like this:
Code:
FIX(x*10)/10
 
Upvote 0
The Fix() eliminated the error. From all that I have read, Fix(x*10)/10 rounds NOT truncates.

In my Excel spreadsheet, using =Trunc((54.3/5)*0.96,1) which truncates to 10.4. The math answer is 10.43. Truncated to one decimal place = 10.4

Using the Fix(((54.3/5)*0.96)/10) gives 10.0.

How can I rectify this?
 
Upvote 0
Using the Fix(((54.3/5)*0.96)/10) gives 10.0.

This formula actually returns 1.

Joe4's solution was Fix(x*10)/10.

You have x= 54.3/5*0.96

So you need Fix(54.3/5*0.96*10)/10 = 10.4, i.e truncated (not rounded) to 1 dp as required.
 
Upvote 0
From all that I have read, Fix(x*10)/10 rounds NOT truncates.
That is not correct. FIX returns the INTEGER portion of a number. It does not round.
However, you want to return the first decimal. Since FIX returns the Integer portion (no decimals), we need to first multiply by 10, then apply the FIX function.
Then, after we get the answer, we can divide by 10 to get it back to the decimal number.

As Stephen pointed out, you had applied it incorrectly to your example. You did not multiply your formula by 10 before applying the FIX function.
 
Upvote 0
No worries! You are welcome.
 
Upvote 0

Forum statistics

Threads
1,224,905
Messages
6,181,661
Members
453,059
Latest member
jkevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top