I have come across this bit of vb.net code:
Protected Sub PremiumCalculation(grossPremiumIncldIpt As Decimal, netPremium As Decimal, ipt As Decimal, commission As Decimal)
Dim needCalcGrossPrem As Boolean = grossPremiumIncldIpt = Decimal.MinValue
Yet the compiler does not even try to assign a decimal to a Boolean !
Furthermore the app will run with no errors whatsoever! Even if I have Option Strict On` at the top of the file !
I have never ever seen a line of code like this before and can't understand why it isn't an invalid type cast or why it works.
Has anyone else encountered this, if so could they explain :S
Related
I am trying to make a calculator app in swift as a practise for my IOS development course. for that app I am trying to append a number to an existing double or integer after the user pressed on a specific button, but I don't know how.
if the user pressed on lets say a 5, then I want that the code should append that 5 to the numbers he pressed before
for instance:
the user has typed the following number:
6797.890
and now he wants to add the 5 to the existing number so that the number would be:
6797.8905
I really don't know how to do this in the code, and I really appreciate it if someone could help me by showing how or giving some resource website's for this problem
thanks a lot!
Benji
I know you mentioned that you are supposed to use an Int or Double, but I would try and use NSDecimalNumber and String if you can. It will give you the precision you want and has a convenient way to turn the string into an NSDecimalNumber, NSDecimalNumber(string: "6797.8905"), as well as back to a string, number.stringValue. You can enteredAmount != NSDecimalNumber.notANumber just in case your user input is incompatible.
You must process the input as a string. If you process it as a double, the trailing zero will be lost and your code will not be able to distinguish between adding "5" to 6797.890 and adding "5" to 6797.89
Only convert from String to Double in one direction (from input to data) after displaying the initial value.
I'm trying to convert this NVARCHAR value into a periodeId.
The Raw data could be '12-02'.
My solution for this was first to try this
(1000+CONVERT(INT,LEFT(2,T1.PERIOD_NAME)))*100+CONVERT(Int,RIGHT(2,t1.PERIOD_NAME))
But i get the same error message here and could find any quick solution for it.
I also tried to just do a simple
LEFT(2,T1.PERIOD_NAME) to see if it was the formula itself that crashed it, but the same error came up.
If you want '12-02' to be 1202, then use replace() to remove the hyphen before conversion:
select cast(replace(period_name, '-', '') as int)
In SQL Server 2012+, you should use try_convert(), in case there are other unexpected values.
You can try:
SELECT (1000+CONVERT(INT,LEFT(t1.PERIOD_NAME,2)))*100+CONVERT(Int,RIGHT(t1.PERIOD_NAME,2))
The character_expression that LEFT operates on is at first place, whereas the integer expression that specifies how many characters of the character_expression will be returned, comes at second place.
I seem to have a problem with a very simple scenario.
I'm trying to add values captured by two text fields (called T1 and T2) and display their total upon pressing a button (GoButton) on a label (Label1).
I tried wording the syntax in multiple ways and it still doesn't work. I feel that some of the syntax I found on the web didn't work for me. I use Xcode 6.3 on Yosemite.
Screenshot:
Is there a chance that there is something I'm missing with my Xcode to accept swift syntax? Please help.
Dante-
There's still a chance the values could be nil. You've correctly !'ed the TextFields so they unwrap automatically, but the Int conversion is also Optional (the Int still thinks it may get a nil value).
Label1.text = "\(T1.text.toInt()! + T2.text.toInt()!)"
Helpful Hint- If you paste your code in here (rather than a screenshot) it's much easier for folks to copy and paste your code into their IDE and test it out.
For those here smarter than me (everyone) I'm curious why Xcode doesn't complain about a single Int conversion:
Label1.text = "\(T1.text.toInt())" // no complaint from the compiler
Value T1.text.toInt() is an Optional Integer. So you must unwrap it first. So use Label1.text = "\(T1.text.toInt()! + T2.text.toInt()!)"
Good luck
Thats because toInt() returns an optional value. You can cast your String to NSString and extract the integer value without returning an optional.
Label1.text = ((T1.text! as NSString).integerValue + (T2.text! as NSString).integerValue + (T3.text! as NSString).integerValue + (T4.text! as NSString).integerValue).description
Label1.text = "\(T1.text.toInt()! + T2.text.toInt()!)" //T1.text.toInt()
is an optional so you should use ! mark otherwise it will return nil
value
I am trying to assign a large value to a double datatype for example
double someValue = 36028797018963967D;
but the problem is, when I debug the value of variable someValue, it becomes 36028797018963968D and I cant figure out why.
Any help will be appreciated,
Thanks!
Both of these numbers are the same in 64 bit floating point. What gets printed is an artifact of the method used to print them. Try this:
assert(36028797018963967D == 36028797018963968D)
It works!
I want to print a simple statement
print (1=1), i expect the result to be TRUE or 1 but sql server tell me:
Incorrect syntax near '='.
why is that?
Same will happen for a statement like that
declare #test bit
set #test = (1=1)
in summary how can i "see" what is returned from a comparison without using an IF statement
Update: The reason i'm asking is because i'm trying to debug why the following statement
declare #AgingAmount smallint
set #AgingAmount = 500
select Amount, datediff(day,Batch.SubmitDate,getdate()) as Aging from myreporrt
where datediff(day,Batch.SubmitDate,getdate()) > #AgingAmount
will return all rows even with aging of 300
so i wanted to test if datediff(day,datesubmited,getdate()) > 500 returns true or false but could not find a way how to display the result of this comparison.
Although SQL Server has the concept of aboolean type, and it understands expressions that resolve to a boolean in IF and WHERE clauses, it does not support declaring boolean variables or parameters. The bit data type cannot store the result of a boolean expression directly, even though it looks suspiciously like one.
The nearest you can get to a boolean data type is this:
-- Store the result of a boolean test.
declare #result bit
select #result = case when <boolean expression> then 1 else 0 end
-- Make use of the above result somewhere else.
if #result = 1
...
else
...
To add to the confusion, SQL Server Management Studio treats bit like boolean when displaying results, and ADO.NET maps bit to System.Boolean when passing data back and forth.
Update: To answer your latest question, use the case when ... then 1 else 0 end syntax in the select statement.