VB script math function - math

I have an ASP page where I have 2 variables, strActualRate and strProposed.
The values are:
strActualRate = 33.30
strProposed = 33.3
So when I write the following line to compare:
if strActualRate <> strProposed then
Response.Writr "Both are not equal!"
end if
I am getting the output "Both are not equal", even though both are the same.
I am sure that I need to use some mathematical conversion function to compare.
Can anyone tell me how to solve this ?
Thanks in advance!

If I understand correctly, you think the two values are equal but because VBScript is comparing strings rather than numbers the two are coming back as not equal.
You're correct in the conversion idea, and here's the code:
if CDbl(strActualRate) <> CDbl(strProposed) then
Response.Write "Both are not equal!"
end if
That will convert your string values to numbers to do the comparison.

Your question doesn't really add up, so I'm not really sure what the problem is. I will try to clear up some things about data types and comparison.
You are using the prefix "str" for your variables which suggests that you intend to store string values in them, however you are instead storing numeric values in them. Either you are confused about how hungarian notation is used to keep track of the data type, or the code that you posted does not look like the code that you are actually using.
The numeric value 33.30 is exactly the same as the value 33.3. If you instead would have used the string values "33.30" and "33.3", they would be two strings that are not equal.
If your code is corrected (Response.Write instead of Response.Writr) so that it runs, it will not produce any output at all. As the values are equal, the condifion in the if statement evaluates to false.
If you do in fact assign string values to the variables, the code would output "Both are not equal!". This is just as expected as the strings are not equal. If you have strings and want to compare them as numerical values, you have to comvert them:
If CDbl(strActualRate) <> CDbl(strProposed) Then
Response.Write "Both are not equal!"
End If

Try casting the values to a double in the comparison statement with CDbl()

Are you intending to perform the comparison as strings, floating point numbers or some other method? If you are comparing them as strings, then clearly they are not equal, as one of them has an extra zero on the end. If you are comparing them as floating point numbers, then you generally want to use a comparison that involves taking the difference and checking that it is smaller than some small value. This is because floating point number calculations involve some degree of inaccuracy and comparisons between them can fail because of the underlying representation.

Related

How to add empty values in a vector, matrix, structure

I am trying to do some calculations where I divide two vectors. Sometimes I encounter a division by zero, which cannot take place. Instead of attempting this division, I would like to store an empty element in the output.
The question is: how do I do this? Can vectors have empty fields? Can a structure be the solution to my problem or what else should I use?
No, there must be something in the memory slot. Simply store a NaN or INT_MIN for integer values.

How to compare two hex values in drools

I need to compare two hex values in drools.
for eg: compare 0xbadf00d with 0xbadf00e
This should result in false as d doesn't match with e.
So my question is, can hex be treated as string value and same comparisons can be made, or there is some other way.
I tried googling but no use.
When using ASCII, the natural order of the digits and letters of an HEX is ascending. This makes the comparison of these values as Strings trivial (assuming they are left-padded with 0s and using the same case).
As an example, if you have an Input class with a hex attribute of type String you can write something like this:
rule "Test"
when
$i1: Input()
$i2: Input(hex > $i1.hex)
then
//Do whatever you need here
end
Hope it helps,

Compare two NSStrings and check for spelling mistakes or missing letters

I would like to compare two strings, one which the user enters into a UITextField and another which I have stored (I know how to do this).
However I would like to notify the user if they have misspelt the string they have entered compared to the stored string, could anyone shed some light on how to do this comparison.
The Identifying and Comparing Strings section of the NSString Class Reference provides methods exactly for that. Please check.
I believe in your case, the following methods would be helpful,
1) isEqualToString: example, [string1 isEqualToString:string2] returns YES if the strings are same, else NO
2) caseInsensitiveCompare: example [string1 caseInsensitiveCompare:string2] compares the strings, regardless of its case. Returns an NSComparisonResult. If it is NSOrderedSame, then the strings match, else they dont.
3) compare:options: example [string1 compare:string2 options:NSDiacriticInsensitiveSearch] compares the strings, with options to compare such as an NSDiacriticInsensitiveSearch where the search treats characters such as 'รถ' and 'o' as same, NSNumericSearch where the numbers within the string are compared using numeric values and so on. (Refer the Search and Comparison Options of the NSString Class Reference)

Weird behaviour on Numeric Stepper decimals

I am making an editor for a field with numbers. I tried a text field, but since it's a Number datatype coming in, it didn't go smoothly -- despite recasting strings as numbers etc.. it kept giving me NaN as the value. So I decided it would be best to go with a numeric stepper.
When I initially loaded it up it would drop all my decimals and only display my numbers as integers. I changed the stepIncrement to 0.1 and now it does show the decimals (a weird requirement imo).. but when I step up it occasionally gives me a value like '17.700000000000003' when I would expect 17.7. All of the numbers in my data have a single decimal place. I know I can write a dataformatter, but it seems like it shouldn't be necessary in this situation.
Is there another way I could deal with this?
You've stumbled upon the compromise of trying to represent decimal numbers in floating point binary formats like IEEE 754. Not all decimal numbers can be exactly represented. You can read up on this issue in great detail here:
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
You can use Number.toFixed(fractionDigits:uint) to display to an arbitrary number of decimal places.
You can use the valueFormatFunction which takes the numeric value and formats it to a string. You will need to set explicit widths on your numeric steppers to make they fit though.
in your MXML
<s:NumericStepper valueFormatFunction="stepperFormatter"/>
in your script
protected function stepperFormatter(newValue:Number):String
{
return Math.ceil(newValue).toString()
}

Decimal places problem with SQLite

I have a SQLite3 table with a column having format DECIMAL(7,2), but whenever I select rows with values not having a non-zero 2nd decimal place (eg. 3.00 or 3.10), the result always has trailing zero(s) missing (eg. 3 or 3.1). Is there any way that I can apply a formatting function in the SELECT statement so that I get the required 2dp? I have tried ROUND(), but this has no effect. Otherwise I have to keep converting the resulting column values into the required format for display (using Python in my case) every time I do a SELECT statement, which is a real pain.
I don't even mind if the result is string instead of numeric, as long as it has the right number of decimal places.
Any help would be appreciated.
Alan
SQLite internally uses IEEE binary floating point arithmetic, which truly does not lend itself well to maintaining a particular number of decimals. To get that type of decimal handling would require one of:
Fixed point math, or
IEEE decimal floating point (rather uncommon), or
Handling everything as strings.
Formatting the values (converting from floating point to string) after extraction is the simplest way to implement things. You could even hide that inside some sort of wrapper so that the rest of the code doesn't have to deal with the consequences. But if you're going to do arithmetic on the value afterwards then you're better off not formatting and instead working with the value as returned by the query, because the format and reconvert back to binary floating point (which Python uses, just like the vast majority of other modern languages) loses lots of information in the reduced precision.

Resources