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
286
votes
6 answers

How to cast int to enum in C++?

How do I cast an int to an enum in C++? For example: enum Test { A, B }; int a = 1; How do I convert a to type Test::A?
user1509260
  • 3,353
  • 3
  • 18
  • 9
285
votes
7 answers

Shorter syntax for casting from a List to a List?

I know it's possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List ListOfY = new List(); foreach(X x in ListOfX) …
Jimbo
  • 22,379
  • 42
  • 117
  • 159
283
votes
7 answers

What is the difference between static_cast<> and C style casting?

Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is there any sort of speed difference?
dicroce
  • 45,396
  • 28
  • 101
  • 140
272
votes
8 answers

Casting a number to a string in TypeScript

Which is the the best way (if there is one) to cast from number to string in Typescript? var page_number:number = 3; window.location.hash = page_number; In this case the compiler throws the error: Type 'number' is not assignable to type…
Ma Jerez
  • 4,887
  • 3
  • 23
  • 21
265
votes
22 answers

How to cast an Object to an int

How can I cast an Object to an int in java?
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
258
votes
8 answers

Fastest way to convert string to integer in PHP

Using PHP, what's the fastest way to convert a string like this: "123" to an integer? Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?
nickf
  • 537,072
  • 198
  • 649
  • 721
256
votes
14 answers

How do I convert from int to Long in Java?

I keep finding both on here and Google people having troubles going from long to int and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int to Long. The only other answers I've found…
Ghosty
  • 3,203
  • 2
  • 18
  • 13
242
votes
4 answers

Haskell: Converting Int to String

I know you can convert a String to an number with read: Prelude> read "3" :: Int 3 Prelude> read "3" :: Double 3.0 But how do you grab the String representation of an Int value?
Squirrelsama
  • 5,480
  • 4
  • 28
  • 38
238
votes
2 answers

SQL: Subtracting 1 day from a timestamp date

I am using Datagrip for Postgresql. I have a table with a date field in timestamp format (ex: 2016-11-01 00:00:00). I want to be able to: apply a mathematical operator to subtract 1 day filter it based on a time window of today-130 days display…
J-Ko
  • 2,503
  • 3
  • 9
  • 6
238
votes
31 answers

TypeScript enum to object array

I have an enum defined this way: export enum GoalProgressMeasurements { Percentage = 1, Numeric_Target = 2, Completed_Tasks = 3, Average_Milestone_Progress = 4, Not_Measured = 5 } However, I'd like it to be represented as an…
AnimaSola
  • 7,146
  • 14
  • 43
  • 62
227
votes
15 answers

Convert Float to Int in Swift

I want to convert a Float to an Int in Swift. Basic casting like this does not work because these types are not primitives, unlike floats and ints in Objective-C var float: Float = 2.2 var integer: Int = float as Float But this produces the…
kev
  • 7,712
  • 11
  • 30
  • 41
219
votes
9 answers

Change type of varchar field to integer: "cannot be cast automatically to type integer"

I have a small table and a certain field contains the type "character varying". I'm trying to change it to "Integer" but it gives an error that casting is not possible. Is there a way around this or should I just create another table and bring the…
itsols
  • 5,406
  • 7
  • 51
  • 95
217
votes
13 answers

String was not recognized as a valid DateTime " format dd/MM/yyyy"

I am trying to convert my string formatted value to date type with format dd/MM/yyyy. this.Text="22/11/2009"; DateTime date = DateTime.Parse(this.Text); What is the problem ? It has a second override which asks for IFormatProvider. What is this?…
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
211
votes
9 answers

Implicit type conversion rules in C++ operators

I want to be better about knowing when I should cast. What are the implicit type conversion rules in C++ when adding, multiplying, etc. For example, int + float = ? int * float = ? float * int = ? int / float = ? float / int = ? int / int = ? int ^…
Matt Montag
  • 7,105
  • 8
  • 41
  • 47
202
votes
12 answers

Downcasting in Java

Upcasting is allowed in Java, however downcasting gives a compile error. The compile error can be removed by adding a cast but would anyway break at the runtime. In this case why Java allows downcasting if it cannot be executed at the runtime? Is…
Warrior
  • 39,156
  • 44
  • 139
  • 214