Go Back   DeveloperBarn Forums > Programming & Scripting > ASP Development

Sponsored Links

Discuss "convert problem" in the ASP Development forum.

ASP Development - Learn coding practices and tips to get the best out of your Active Server Pages (ASP). The Classic ASP forum is for ASP/VBScript and ASP/JScript applications.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old June 26th, 2008, 11:39 PM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default convert problem

Hi,

I have this variable called strwt it can hold numbers and decimal values

IF it holds decimal values like this

strwt="134.3"


then i want the value in strwt to be

strwt="134"

But if the decimal value is .6 or .5 then i want the variable to hold

strwt="135"

But if the strwt value is 132 or the whole number then i want it to stay as it is

can someone tell me how i can do this.

Todd
Reply With Quote
Sponsored Links
  #2  
Old June 27th, 2008, 04:08 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

I tried using round(strwt) but that seemed to ruond down any .5, so I came up with this little function.
Code:
function round_no(no)
if instr(cstr(no),".")=0 then
     round_no = no
else
     Dim arr_no, dec_no
     arr_no = split(cstr(no),".")
     dec_no = arr_no(ubound(arr_no))
     if dec_no <=4 then
           round_no = fix(no)
     else
           round_no = fix(no)+1
     end if
end if
 
end function
Then to call it, just use:-
Code:
round_no(strwt)
I would have thought they'd be a built in function for this, but I couldn't find it.

Hope that helps.
Reply With Quote
  #3  
Old June 27th, 2008, 07:35 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

@RR - the Round() function allows you to specify how many decimal places to round off to.

The syntax is: Round(Number, NumDecimalPlaces)

The second argument is optiononal.

Just FYI...
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #4  
Old June 27th, 2008, 07:44 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Quote:
Originally Posted by jmurrayhead View Post
@RR - the Round() function allows you to specify how many decimal places to round off to.

The syntax is: Round(Number, NumDecimalPlaces)

The second argument is optiononal.

Just FYI...
Yeah, I know. It's just that he wanted it to zero decimal places so I left it off.

Any idea why it rounds .5 down rather than up?
Reply With Quote
  #5  
Old June 27th, 2008, 07:53 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Quote:
Originally Posted by richyrich View Post
Yeah, I know. It's just that he wanted it to zero decimal places so I left it off.

Any idea why it rounds .5 down rather than up?
Not sure...basic math says anything that is 5 or more you round up. Found this, however with a more simple rounding function: How do I round a number *properly* with VBScript?

Code:
    Function roundit(number,decPoints)  
        decPoints = 10^decPoints  
        roundit = round(number*decPoints+0.1)/decPoints 
    End Function
Reply With Quote
  #6  
Old June 27th, 2008, 03:47 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

It appears that round() conforms to the ISO Standard in that x.5 rounds to an even number.
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > ASP Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -4. The time now is 03:39 PM.



Content Relevant URLs by vBSEO 3.2.0