Cast Double to Integer in Java
You need to explicitly get the int value using method intValue() like this: Double d = 5.25; Integer i = d.intValue(); // i becomes 5 Or double d = 5.25; int i = (int) d;
You need to explicitly get the int value using method intValue() like this: Double d = 5.25; Integer i = d.intValue(); // i becomes 5 Or double d = 5.25; int i = (int) d;
The answer below the line was written in 2008. C# 7 introduced pattern matching, which has largely replaced the as operator, as you can now write: if (randomObject is TargetType tt) { // Use tt here } Note that tt is still in scope after this, but not definitely assigned. (It is definitely assigned within … Read more
int myInt = myBoolean ? 1 : 0; ^^ PS : true = 1 and false = 0
This method was posted by @lauthiamkok in the comments. I’m posting it here as an answer to call more attention to it. Depending on your needs, you should consider using filter_var() with the FILTER_VALIDATE_BOOLEAN flag. filter_var( true, FILTER_VALIDATE_BOOLEAN); // true filter_var( ‘true’, FILTER_VALIDATE_BOOLEAN); // true filter_var( 1, FILTER_VALIDATE_BOOLEAN); // true filter_var( ‘1’, FILTER_VALIDATE_BOOLEAN); // true … Read more
In the simplest case, it’s probably sufficient to “cast” the array as an object: $object = (object) $array; Another option would be to instantiate a standard class as a variable, and loop through your array while re-assigning the values: $object = new stdClass(); foreach ($array as $key => $value) { $object->$key = $value; } As … Read more
A new method has been added with Java 8 to do just that. import static java.lang.Math.toIntExact; long foo = 10L; int bar = toIntExact(foo); Will throw an ArithmeticException in case of overflow. See: Math.toIntExact(long) Several other overflow safe methods have been added to Java 8. They end with exact. Examples: Math.incrementExact(long) Math.subtractExact(long, long) Math.decrementExact(long) Math.negateExact(long), … Read more
For non-negative (unsigned) integers only, use isdigit(): >>> a = “03523” >>> a.isdigit() True >>> b = “963spam” >>> b.isdigit() False Documentation for isdigit(): Python2, Python3 For Python 2 Unicode strings: isnumeric().
The C++ standard guarantees the following: static_casting a pointer to and from void* preserves the address. That is, in the following, a, b and c all point to the same address: int* a = new int(); void* b = static_cast<void*>(a); int* c = static_cast<int*>(b); reinterpret_cast only guarantees that if you cast a pointer to a … Read more
You can use the strval() function to convert a number to a string. From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context. $var = 5; // Inline variable parsing echo “I’d like {$var} waffles”; // = … Read more
Converting Int to String: let x : Int = 42 var myString = String(x) And the other way around – converting String to Int: let myString : String = “42” let x: Int? = myString.toInt() if (x != nil) { // Successfully converted String to Int } Or if you’re using Swift 2 or 3: … Read more