Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What function used to round to 2 significant digits 1

Status
Not open for further replies.

maupiti

Programmer
Oct 27, 2003
240
US
What function used to round to 2 significant digits for Double data type ?

Percent_Of_Total = (object_File / Folder_Size) * 100

I tried these, but it won 't work

Percent_Of_Total = Round((object_File / Folder_Size) * 100, 2)

Percent_Of_Total = CDbl ((object_File / Folder_Size) * 100, 2)
 
This one should work...

[tt]
Percent_Of_Total = Val(Format((object_file / Folder_Size) * 100, "0.00"))
[/tt]

Hope this helps,


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Why Thank You

jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Hey JAG
Maybe i'm sounding stoooopid but what does the "val" do?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Straight from the MSDN help file:

val:

Returns the numbers contained in a string as a numeric value of appropriate type.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Ish...my bad...
thanks ca

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
You shouldn't use it except for Integers, or returning numbers with decimals which uses the US decimal seperator only.
 
CClint,

I'm intrigued, why?



jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
As said.

Change the decimal seperator in your region settings for numbers, to something that some European country would use, such as a comma.

Now, try this:

Dim dtest As Double

'hard coded number. A string in the countries number format, CDbl("1,23456"), would produce the same.
dtest=cdbl(1.23456)

Percent_Of_Total = Val(Format((dtest) * 100, "0.00"))

The result will be
123
and not
123.46
as expected

So, it will only work when region settings for number decimal seperators are set to "US" format.
You will only get 1.00, naturally.

 

>You will only get 1.00, naturally.

Ignore that line. I started the post with a different example, but changed it and overlooked this last line.
 
OK, Well done that man...

Modified code as follows.

[tt]
Dim iPercent As String

iPercent = Format((object_file / Folder_Size) * 100, "0.00")
'Replace any Commas
iPercent = Replace(iPercent, ",", ".")

Percent_Of_Total = Val(iPercent)
[/tt]

This would work, yes?

CC, Thank you for the pointers, more comments would be welcomed.

jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Another language I use has a bizarre Round function that rounds to the nearest even number. Had to write my own rounding function which as it happens will work in VB too.
Code:
Public Function fTrueRound(dblNum As Double, intPrec As Integer) As Double
	
	'---- Rounds dblNum to the precision specified in intPrec
	
	dblNum = dblNum * 10 ^ intPrec
	If dblNum < 0 Then
		dblNum = Fix(dblNum - .5)
	Else
		dblNum = Int(dblNum + .5)
	End If
	dblNum = dblNum / 10 ^ intPrec
	
	fTrueRound = dblNum
	
End Function
So you should be able to do something like:

Percent_Of_Total = fTrueRound(object_File / Folder_Size * 100, 2)

Paul Bent
Northwind IT Systems
 
>This would work, yes?

Nope.

Don't use Val() at all for numbers with decimals...
Just use:
Percent_Of_Total = Format((object_file / Folder_Size) * 100, &quot;0.00&quot;)
And do not use the Replace funtion like this!

If you calculate using number variables or numeric conversion functions, (or hard coded numbers using the &quot;US&quot; decimal separator), everything will be fine, until you pass the value to something like an SQL statement (here, use the Str() function on numbers).

If you need to convert the results to a numeric variable, then use something like CCur(), CDec() or whatever, depending on the decimal places needed (I say 'depending on the decimal places needed' because of undesired rounding)

You ca also do something like this (but not needed in most cases):

Percent_Of_Total = (Int((object_file / Folder_Size) * 100 * 10 ^ 2)) / 10 ^ 2

replacing 10 ^ 2 with 10 ^ n depending on how many decimal places to format it to.


 
paulbent,
We cross posted.

We've had several discussions on this:

Thread222-376792
thread222-376571

Try this with your round function:
fTrueRound(4.989185 ,5)
fTrueRound(4.989195 ,5)
fTrueRound(4.989196 ,5)

The error is due to the use of the double.
Using CDec() will work &quot;better&quot; up to 15 digits.


The Format function works &quot;right&quot; for these cases:
?Format$(4.989185, &quot;0.00000&quot;)
?Format$(4.989195, &quot;0.00000&quot;)
?Format$(4.989196, &quot;0.00000&quot;)

 
CCLINT,

Thanks for the heads up. I must say that in VB I generally use the intrinsic Round function but when I just did a quick comparison using your example, one of the answers was incorrect from each function:
Code:
s/be     Round    fTrueRound
4.98919  4.98918  4.98919
4.9892   4.98919  4.98919
4.9892   4.9892   4.9892


Paul Bent
Northwind IT Systems
 
Oh, if you are using Round just check the following:

round(4.5)
round(5.5)
 
CClint,

Thanks for the pointers. I probably use the Val Statement too much...

Have a Furry Pink Star on me!


jgjge3.gif
[tt]&quot;Very funny, Scotty... Now Beam down my clothes.&quot;[/tt]
 
Hi there everyone. Thank you for your participation.

Some member were suggesting using the round function. I did use it but it gives me an error.
1) How come VB5 does not recognize the function round like
Percent_Of_Total = Round((object_File / Folder_Size) * 100,
2)

2) There has been alot of answer given. What is the final verdict ? What is the best rounding method for general use where it appled to most cases. Perhaps the one that JAG14 suggested.

=========================================
Dim iPercent As String

iPercent = Format((object_file / Folder_Size) * 100, &quot;0.00&quot;)
'Replace any Commas
iPercent = Replace(iPercent, &quot;,&quot;, &quot;.&quot;)
Percent_Of_Total = Val(iPercent)
==========================================
 

Yes, use the Format() function to round with when the total number of digits (ALL digits) to round to are not more than 15.

Ignore the following if the reasons (which I understand them to be) do not interest anyone.

It, the Format() function, will round up to a total of 15 digits (not decimal places, but TOTAL digits). After that 15 digits, the rest get turned into zeros. The format function uses a Decimal number type for it's accuracy.

It works similar to this:
[blue]
Code:
Public Function RoundX(ByVal dNumber As Double, Optional ByVal lDecimalPlaces As Integer = 2) As Double
    'Can only round up to 15 digits, the integer portion having precedence.
    'Works like the Format$() function.
    Dim xFactor As Double
    xFactor = CDec(10 ^ lDecimalPlaces)
    RoundX = Fix(CDec(dNumber * xFactor + (Sgn(dNumber) * 0.5))) / xFactor
End Function
[black]
(I hope that's the right one)
The problem with using a double is that not all decimals numbers can be represented in binary.
Using a fixed decimal, we can correct this problem up to a certain limit.

The Round() function uses statistical rounding.
This means the following:

1. It is agreed that numbers less than 5 round down.
2. It is agreed that numbers greater than 5 round up.

3. But what about 5? It is exactly &quot;midway&quot;. Rounding either way would be actually incorrect, and when working with statistics, rounding that 5 always up would lead to bad statistics.
We could do this: Round 5 up except every other occurance which we would round it up instead (the first 5 round up, the second round down, the third round up, etc.)

Well, we would need to create a function that would round a GROUP of values in order to keep track of the occurances of 5.

The Round() function works with just ONE value at a time. So what it does is just this:
If the digit before the 5 is an odd number, then round up, otherwise round down. As seen in strongm's post, or the thread I mentioned.
You can see that this would also lead to an inaccuracy when used on a GROUP of values - especially if all values in that group which have a 5 as the last digit to round to, also have as the digit just before the 5, an even digit (or an odd digit, or more evens than odd, etc.)
It's also not correct...just closer to correct statistical rounding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top