Format decimals in VBA function

S

staeri

I input dblTargetValue and dblLowScoreValue as Double into a VBA
function and show the output as a string, like this:

Dim strOutput as string
strOutput = CStr((dblTargetValue - dblLowScoreValue))

I want to round the output so that it doesn't show too many decimals,
like this:
* If dblTargetValue - dblLowScoreValue = 0 decimals, show 0 decimals
* If dblTargetValue - dblLowScoreValue = 1 decimals, show 1 decimal
* If dblTargetValue - dblLowScoreValue = 2 decimals, show 2 decimals
* If dblTargetValue - dblLowScoreValue => 3 decimals, show 3 decimals

I really need help with this immediately because I have a deadline to
keep so I'm very grateful for help!

Regards,

SE
 
B

Bob Phillips

Dim strOutput As String
strOutput = Application.Text(Round(dblTargetValue - dblLowScoreValue, 3),
"General")

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Dim strOutput As String
strOutput = Format(dblTargetValue - dblLowScoreValue, "#,##0.###")

Would be another way.

for example:
? format(123.4,"#,##0.###")
123.4
? format(123.45,"#,##0.###")
123.45
? format(123.456,"#,##0.###")
123.456
? format(123.4563,"#,##0.###")
123.456
? format(123.4567,"#,##0.###")
123.457
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top