# Round off

## Recommended Posts

Hi,

How can you round 1.24 to 1.3 ?

I know if I have value 1.24 I can round it to 1.2 by using Round(\$val, 1). But this is not what I want. What I want is that if my script brings value 1.21 or 1.22 or 1.23 etc, I want it to round off to one place decimal so that it becomes 1.3. On the other hand if the value is 1.20 then I want it to be left at 1.2 . Can somebody assist. Many thanks.

##### Share on other sites

is wat ur script returns ONLY the values of 1.2 - 1.3?

edit?

```If \$Val > 1.2 Then
\$Val = 1.3
Else
\$Val = 1.2
EndIf```
Edited by burrup

qq

##### Share on other sites

is wat ur script returns ONLY the values of 1.2 - 1.3?

edit?

```If \$Val > 1.2 Then
\$Val = 1.3
Else
\$Val = 1.2
EndIf```

<{POST_SNAPBACK}>

Thanks for writing Burrup. To answer your question, my script can bring up any numbers. I only gave an example in my request below, but just to elaborate, my script can be 1.34, or 2.76 or 9.12 or 7.45 etc etc.

##### Share on other sites

edit, one more question ... will the DP only be 2? eg. will \$val = 1.234 or only 2 decimal places, 1.23 ?

Edited by burrup

qq

##### Share on other sites

edit, one more question ... will the DP only be 2? eg. will \$val = 1.234 or only 2 decimal places, 1.23 ?

<{POST_SNAPBACK}>

Good question Burrup, sorry I wasn't specific so let me explain what my script actually does. It gets the CPU speed from the registry eg 2390Mhz. I then have it divided by 1024 and I get 2.3339843Ghz. I would prefer the outcome to be 2.40Ghz. (I know I initially asked for something like 2.4 which is one place decimal; but I thought if I could get an idea then I have something to work on). Anyway it would save me some time if you could help me with rounding off to two decimal keeping in mind that its CPU speed that I want to display

##### Share on other sites

```\$Val = 1.21

\$Trim_Left = StringTrimLeft (\$val,3)
\$Str_Left = StringLeft (\$Val,2)
If \$Trim_Left > 0 Then
\$Trim_Left_2 = StringTrimLeft (\$Val,2)
\$Trim_Right = StringTrimRight (\$Trim_Left_2,1)
\$Dec_Right = \$Trim_Right + 1
\$Val = \$Str_Left & \$Dec_Right
MsgBox(0,"Value",\$Val)
Else
MsgBox(0,"Value",\$Val)
EndIf```

im sure there is a much much better way lol, and safer ... the above script will work for the numbers 1.0 - 9.90

edit: forgot to say the above only works for 2 decimal places, gimme a sec

```\$Ghz = 2.66838949
\$Val = StringLeft (\$Ghz,4)

\$Trim_Left = StringTrimLeft (\$Val,3)
\$Str_Left = StringLeft (\$Val,2)
\$Trim_Left_2 = StringTrimLeft (\$Val,2)
\$Trim_Right = StringTrimRight (\$Trim_Left_2,1)
\$Dec_Right = \$Trim_Right + 1
\$Val = \$Str_Left & \$Dec_Right

Msgbox(0,"Value",\$Val)```

try that

edit: damnit it has trouble when its 1.9XXXXx

can u tell me the key name where u get the CPU speed from the registry?

Edited by burrup

qq

##### Share on other sites

How can you round 1.24 to 1.3 ?

<{POST_SNAPBACK}>

The following function _RoundAlwaysUp will round a real value as you describe (to a specified number of decimal plaecs). A separate function should be written to pad the number with trailing zeros as in you subsequent post.

```; For testing purposes

; Test rounding to 1 places
For \$i = 0 To 4
MsgBox(4096, "1 place", \$dNum & " = " & _RoundAlwaysUp(\$dNum, 1))
Next; \$i

; Test rounding to 3 places
For \$i = 0 To 4
MsgBox(4096, "3 places", \$dNum & " = " & _RoundAlwaysUp(\$dNum, 3))
Next; \$i
Exit```

```Func _RoundAlwaysUp(\$dVal, \$iPlaces)
; Round off the decimal portion of a real number upward,
; never downward, and return the rounded number.
; \$dVal   = The real number to be rounded.
; \$iPlaces = The number of decimal places in the rounded number.
\$dNewVal = Round(\$dVal, \$iPlaces)
If \$dVal - \$dNewVal > 0 Then
\$dXXX = Number("0." & StringMid("0000000000000000", 1, \$iPlaces - 1) & "1")
\$dNewVal = \$dNewVal + \$dXXX
EndIf
Return \$dNewVal
EndFunc  ;==>_RoundAlwaysUp```

Phillip

##### Share on other sites

Burrup,

I tried both your suggestions and it works except (as you pointed out) if the value is eg 1.999999 it outputs this to 1.10. So I cannot use this. But just to answer your question, the CPU is found in HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "~MHz"

1.9999999 outputs to 2

2.3339843 outputs to 2.4

So this will be the way for me. Thanks guys for you help!

##### Share on other sites

1.9999999 outputs to 2

2.3339843 outputs to 2.4

So this will be the way for me. Thanks guys for you help!

<{POST_SNAPBACK}>

You're welcome.

Phillip

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...