Page Index Toggle Pages: 1 [2]  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Money Format (Read 4086 times)
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Money Format
Reply #15 - Apr 2nd, 2005 at 10:07pm
Print Post Print Post  
Quote:
You might consider posting this to the Programming  Examples board and/or submitting it to Inside Sesame as a Tip.
Good idea.

Quote:
1. The return type of the Function is declared, so it does not accidentally get forced to an Integer.
Noted and changed. Smiley

Quote:
2. A separate variable is used to return, rather than taking over an argument. The results of returning a passed in argument can be unpredicatable.
The example on page 70 of the programming guide returns "amount", which is a passed in variable. ???


Erika,

Thanks for your input. Although I didn't notice any anomalies, I have updated the code below to reflect your suggestions.

Code
Select All
//########## Begin Custom Rounding Function ##########
Function @cuRnd(Value as double, Places as int) as double

var vNeg as int		//Used to handle Negatives properly
var vStr as string

if Value < 0
	vNeg = -1
else
	vNeg = 1

vStr = @Str(@Abs(Value))

if @Instr(vStr, ".") > 0	//Protects whole numbers when rounding to zero places
	{
	if @Mid(vStr, @Instr(vStr, ".") + Places + 1, 1) >= 5
		vStr = @Str( @TN(vStr) + (1 / @Exp(10, Places)) )
	vStr = @Del(vStr, @Instr(vStr, ".") + Places + 1, 10)
	}

vStr = @Str(@TN(vStr) * vNeg)

Return @TN(vStr)

End Function
//########## End Custom Rounding Function ##########
 



BTW, I have a slightly unrelated question. What is the difference between @ToMoney and @ToNumber? They both return a double, and seem to return the same number of digits after the decimal.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Money Format
Reply #16 - Apr 3rd, 2005 at 2:12pm
Print Post Print Post  
Quote:
The example on page 70 of the programming guide returns "amount", which is a passed in variable. ???


No, it returns (amount * 16), which is the result of a calculation using the variable, not the variable itself. I can see where you might find that confusing though.   

Quote:
BTW, I have a slightly unrelated question. What is the difference between @ToMoney and @ToNumber? They both return a double, and seem to return the same number of digits after the decimal.


There is currently no difference.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Money Format
Reply #17 - Apr 3rd, 2005 at 6:06pm
Print Post Print Post  
Quote:
No, it returns (amount * 16), which is the result of a calculation using the variable, not the variable itself. I can see where you might find that confusing though.

I see it now. The difference is that my original code was changing the passed in argument (i.e. Value = ...), where the example on page 70 is just using the argument to make a new value to return to the function.


Also, I would like to make the last few lines a bit more efficient. Would it be correct usage if I changed the last few lines from this:

     vStr = @Str(@TN(vStr) * vNeg)

     Return @TN(vStr)

To this:

     Return @TN(vStr) * vNeg

If so, should I also put parentheses around some of it like this:

     Return (@TN(vStr) * vNeg)
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Money Format
Reply #18 - Apr 3rd, 2005 at 6:47pm
Print Post Print Post  
Quote:
Also, I would like to make the last few lines a bit more efficient. Would it be correct usage if I changed the last few lines from this:

     vStr = @Str(@TN(vStr) * vNeg)

     Return @TN(vStr)

To this:

     Return @TN(vStr) * vNeg

If so, should I also put parentheses around some of it like this:

     Return (@TN(vStr) * vNeg)


Your rewrite looks fine. Although, to really use good practices, you should assign the value of the calculation to a double, then return the double.

var vRet as Double 
   
       vRet = @TN(vStr) * vNeg
       Return(vRet)


  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Money Format
Reply #19 - Apr 4th, 2005 at 3:02am
Print Post Print Post  
Quote:
you should assign the value of the calculation to a double, then return the double.

Isn't that taken care of when we assign the function itself to a Double? Otherwise, what's the purpose of assigning the type of function? (I hope I don't sound sarcastic - I really want to know.)

I did some testing (trying to break SBasic Grin) with the following code. Even though I'm returning a string value in the function, the function always gives me an Integer as it is assigned on the first line. Actually, I tried declaring the function as many different types, and it always returned the type that was assigned on the 1st line of the function.

Maybe I just got spoiled with Q&A's forgiveness in this area. Smiley

Code
Select All
Function @TestAsInt(Value as String) as Int

var vStr as String

vStr = Value

Return vStr

End Function
 

  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Money Format
Reply #20 - Apr 4th, 2005 at 12:00pm
Print Post Print Post  
Quote:
Isn't that taken care of when we assign the function itself to a Double? Otherwise, what's the purpose of assigning the type of function? (I hope I don't sound sarcastic - I really want to know.)


It will always return the type assigned by the function declaration. That's what the declaration is for.

The advice I gave you about assigning the result of the calculation to a variable, then returning the variable is "good programming practices" advice, as opposed to "broken code". The less happening on any one line, the easier your code is to read and debug later. This is especially important on a return statement because that is a breaking point to put debug. If the return value is incorrect, you can't tell if it is the result of the calculation or something else, since it is all happening at once.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print