PHP String to Float
$rootbeer = (float) $InvoicedUnits; Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.
$rootbeer = (float) $InvoicedUnits; Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.
There’s no casting in javascript, so you cannot throw if “casting fails”. Typescript supports casting but that’s only for compilation time, and you can do it like this: const toDo = <IToDoDto> req.body; // or const toDo = req.body as IToDoDto; You can check at runtime if the value is valid and if not throw … Read more
No, there’s no built-in way to convert a class like you say. The simplest way to do this would be to do what you suggested: create a DerivedClass(BaseClass) constructor. Other options would basically come out to automate the copying of properties from the base to the derived instance, e.g. using reflection. The code you posted … Read more
Starting with API 26, findViewById uses inference for its return type, so you no longer have to cast. Old definition: View findViewById(int id) New definition: <T extends View> T findViewById(int id) So if your compileSdk is at least 26, it means that you can make use of this 🙂
The syntax for this sort of assignment uses a wildcard: List<SubClass> subs = …; List<? extends BaseClass> bases = subs; It’s important to realize that a List<SubClass> is not interchangeable with a List<BaseClass>. Code that retains a reference to the List<SubClass> will expect every item in the list to be a SubClass. If another part … Read more
Upcasting is casting to a supertype, while downcasting is casting to a subtype. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException. In your case, a cast from a Dog to an Animal is an upcast, because a Dog is-a Animal. In general, you can upcast whenever there is … Read more
In Kotlin, there’s no way to check the generic parameters at runtime in general case (like just checking the items of a List<T>, which is only a special case), so casting a generic type to another with different generic parameters will raise a warning unless the cast lies within variance bounds. There are different solutions, … Read more
I was just wrestling with a similar problem myself, but didn’t want the overhead of a function. I came up with the following query: SELECT myfield::integer FROM mytable WHERE myfield ~ E’^\\d+$’; Postgres shortcuts its conditionals, so you shouldn’t get any non-integers hitting your ::integer cast. It also handles NULL values (they won’t match the … Read more
If you know the actual type, then just: SomeType typed = (SomeType)obj; typed.MyFunction(); If you don’t know the actual type, then: not really, no. You would have to instead use one of: reflection implementing a well-known interface dynamic For example: // reflection obj.GetType().GetMethod(“MyFunction”).Invoke(obj, null); // interface IFoo foo = (IFoo)obj; // where SomeType : IFoo … Read more
Those are all slightly different, and generally have an acceptable usage. var.ToString() is going to give you the string representation of an object, regardless of what type it is. Use this if var is not a string already. CStr(var) is the VB string cast operator. I’m not a VB guy, so I would suggest avoiding … Read more