Function doesn't work
BlitzPlus Forums/BlitzPlus Programming/Function doesn't work
| ||
The following is a Function I wrote to strip decimals from a string of numbers: Function StripDec(Q$) Z=Instr(Q$,".",1) X=Z-1 ; PlaySound(sndBeep) ; WaitKey() A$=Left$(Q$,X) < Puts Parameter must be Positive PlaySound(sndBeep) on screen WaitKey() X=Len(Q$) B$=Right$(Q$,X-Z) Q$=A$+B$ Z=Int(Q$) Z=100 * Z Return Z End Function What parameters could this be looking for? Playsound & waitkeys are NOT apart of Function. Just used them to isolate problem. How can I solve this? |
| ||
result=Int("234.123") or.. if you just want to get rid of the dot, and want all fractional numbers to be a part of the real numbers.. result=Replace$("234.123",".","") :D |
| ||
You are missing two ; to make the text following a command a comment. One ; is required before "< Puts Parameter ..." and the other is required before "on screen" like this:; PlaySound(sndBeep) ; WaitKey() A$=Left$(Q$,X) ;< Puts Parameter must be Positive PlaySound(sndBeep) ;on screen WaitKey() X=Len(Q$) B$=Right$(Q$,X-Z) Q$=A$+B$ Z=Int(Q$) Z=100 * Z Return Z End Function Also, although you can use Int on a String it always rounds strings down this is undesireable for numbers like "1.6", because you'll get 1 not 2! Also note that Int uses bankers rounding i.e. it rounds any numbers that fall exactly on 0.5 to nearest even number i.e. 1.5 = 2 and 2.5 = 2 (not 3). Try this function (originally by Beaker) to do a rounding where .5 always rounds up to neareast integer (never down) Function ccRound(flot#) Return Floor(flot+0.5) End Function |
| ||
I didn't need the ; because I put that explanation in after I pasted it in this post. There is absolutely nothing in this Function that should make an error message come on the screen saying that Parameters must be positive that I can see. There is also no index for error messages any where on the web site or in the manual. If that is to be kept secret why have them at aLL? Frustrated!! |
| ||
Well if it doesn't find a decimal point then Z is -1 I think. There for X is -2, and that would explain your problem. But I'm just guessing. |
| ||
sorry Zooker I didn't get your comment, I do now and Sushimasta is correct. You need more error checking early on. |
| ||
Zooker, check out how to use the [code][/ code] commands to seperate your code from the rest of your post. What are the forum codes? |
| ||
Yet I fail to see why Replace$("99.99",".","") couldn't do the job here.. |
| ||
problem is he's using integers to hold floating point currency values (which is a good idea) but he'll need some of validation before using replace as what if the user enters more than 2 digits after the decimal point e.g. 12.345. This will not result in an amount in cents, it will be in 10ths of cents which is dumb. The string must be parsed to check it has a decimal place (and only one), to check it contains only numbers (no other chars) and if there are more than 2 decimal places, the extra ones must be removed. Then Replace will work fine. |