Jump to content

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.

Link to comment
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.
Link to comment
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
Link to comment
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

Link to comment
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
Local $adNum[6]
$adNum[0] = 1.2
$adNum[1] = 1.24
$adNum[2] = 1.2001
$adNum[3] = 1.2551
$adNum[4] = 1.2991

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

; Test rounding to 3 places
For $i = 0 To 4
   $dNum = $adNum[$i]
   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

Link to comment
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"

phillip123adams, I tried your suggestion and it works perfectly. For example

1.9999999 outputs to 2

2.3339843 outputs to 2.4

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...