Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

19752 questions
543
votes
10 answers

Safely casting long to int in Java

What's the most idiomatic way in Java to verify that a cast from long to int does not lose any information? This is my current implementation: public static int safeLongToInt(long l) { int i = (int)l; if ((long)i != l) { throw new…
Brigham
  • 14,395
  • 3
  • 38
  • 48
483
votes
35 answers

How to convert an array to object in PHP?

How can I convert an array like this to an object? [128] => Array ( [status] => "Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution." ) [129] => Array ( [status] => "The other day at…
streetparade
  • 32,000
  • 37
  • 101
  • 123
453
votes
19 answers

How to convert string to boolean php

How can I convert string to boolean? $string = 'false'; $test_mode_mail = settype($string, 'boolean'); var_dump($test_mode_mail); if($test_mode_mail) echo 'test mode is on.'; it returns, boolean true but it should be boolean false.
Run
  • 54,938
  • 169
  • 450
  • 748
417
votes
18 answers

Casting vs using the 'as' keyword in the CLR

When programming interfaces, I've found I'm doing a lot of casting or object type conversion. Is there a difference between these two methods of conversion? If so, is there a cost difference or how does this affect my program? public interface…
Frank V
  • 25,141
  • 34
  • 106
  • 144
417
votes
12 answers

Convert boolean to int in Java

What is the most accepted way to convert a boolean to an int in Java?
hpique
  • 119,096
  • 131
  • 338
  • 476
384
votes
17 answers

Cast Int to enum in Java

What is the correct way to cast an Int to an enum in Java given the following enum? public enum MyEnum { EnumValue1, EnumValue2 } MyEnum enumValue = (MyEnum) x; //Doesn't work???
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
379
votes
19 answers

Cast Double to Integer in Java

Any way to cast java.lang.Double to java.lang.Integer? It throws an exception "java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
4lex1v
  • 21,367
  • 6
  • 52
  • 86
374
votes
8 answers

Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

I want to extract just the date part from a timestamp in PostgreSQL. I need it to be a postgresql DATE type so I can insert it into another table that expects a DATE value. For example, if I have 2011/05/26 09:00:00, I want 2011/05/26 I tried…
keren
  • 3,807
  • 2
  • 15
  • 7
358
votes
13 answers

Does it make sense to use "as" instead of a cast even if there is no null check?

In development blogs, online code examples and (recently) even a book, I keep stumbling about code like this: var y = x as T; y.SomeMethod(); or, even worse: (x as T).SomeMethod(); That doesn't make sense to me. If you are sure that x is of type…
Heinzi
  • 167,459
  • 57
  • 363
  • 519
346
votes
14 answers

How do I convert all of the items in a list to floats?

I have a script which reads a text file, pulls decimal numbers out of it as strings and places them into a list. So I have this list: my_list = ['0.49', '0.54', '0.54', '0.55', '0.55', '0.54', '0.55', '0.55', '0.54'] How do I convert each of the…
snk
327
votes
14 answers

Convert String to double in Java

How can I convert a String such as "12.34" to a double in Java?
TinyBelly
  • 3,337
  • 3
  • 17
  • 7
326
votes
14 answers

C# : 'is' keyword and checking for Not

This is a silly question, but you can use this code to check if something is a particular type... if (child is IContainer) { //.... Is there a more elegant way to check for the "NOT" instance? if (!(child is IContainer)) { //A little ugly... silly,…
hugoware
  • 35,731
  • 24
  • 60
  • 70
308
votes
20 answers

How do you cast a List of supertypes to a List of subtypes?

For example, lets say you have two classes: public class TestA {} public class TestB extends TestA{} I have a method that returns a List and I would like to cast all the objects in that list to TestB so that I end up with a List.
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
306
votes
11 answers

Integer division: How do you produce a double?

For this code block: int num = 5; int denom = 7; double d = num / denom; the value of d is 0.0. It can be forced to work by casting: double d = ((double) num) / denom; But is there another way to get the correct double result? I don't like casting…
walnutmon
  • 5,873
  • 7
  • 42
  • 57
286
votes
3 answers

TypeScript or JavaScript type casting

How does one handle type casting in TypeScript or Javascript? Say I have the following TypeScript code: module Symbology { export class SymbolFactory { createStyle( symbolInfo : SymbolInfo) : any { if (symbolInfo ==…
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185