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
3856
votes
32 answers

How do I cast int to enum in C#?

How do I cast an int to an enum in C#?
lomaxx
  • 113,627
  • 57
  • 144
  • 179
3837
votes
12 answers

Why don't Java's +=, -=, *=, /= compound assignment operators require casting long to int?

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for…
3075
votes
12 answers

When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast (type)value (C-style cast) type(value) (function-style cast) How does one decide which to use in which specific cases?
e.James
  • 116,942
  • 41
  • 177
  • 214
2766
votes
29 answers

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(*sieve) * length); rather than: int *sieve = (int *) malloc(sizeof(*sieve) * length); Why would this…
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
2314
votes
31 answers

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this. public enum Question { Role = 2, ProjectFunding = 3, TotalEmployee = 4, NumberOfServers = 5, …
jim
  • 26,598
  • 13
  • 51
  • 66
2025
votes
8 answers

Regular cast vs. static_cast vs. dynamic_cast

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e. MyClass *m = (MyClass *)ptr; all over the place, but there seem to be two…
Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
1947
votes
41 answers

How do I check if a string represents a number (float or int)?

How do I check if a string represents a numeric value in Python? def is_number(s): try: float(s) return True except ValueError: return False The above works, but it seems clunky. If what you are testing comes from…
Daniel Goldberg
  • 19,908
  • 4
  • 21
  • 29
1475
votes
16 answers

Change column type in pandas

I created a DataFrame from a list of lists: table = [ ['a', '1.2', '4.2' ], ['b', '70', '0.03'], ['x', '5', '0' ], ] df = pd.DataFrame(table) How do I convert the columns to specific types? In this case, I want to convert…
user1642513
916
votes
35 answers

How do I convert a string to a number in PHP?

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP? Input Output '2' 2 '2.34' 2.34 '0.3454545' …
Sara
  • 14,098
  • 13
  • 34
  • 50
855
votes
16 answers

Direct casting vs 'as' operator?

Consider the following code: void Handler(object o, EventArgs e) { // I swear o is a string string s = (string)o; // 1 //-OR- string s = o as string; // 2 // -OR- string s = o.ToString(); // 3 } What is the difference between the…
nullDev
  • 11,170
  • 8
  • 34
  • 52
803
votes
9 answers

Why use static_cast(x) instead of (int)x?

I've heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?
Tommy Herbert
  • 20,407
  • 14
  • 52
  • 57
702
votes
14 answers

How to convert a factor to integer\numeric without loss of information?

When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers. f <- factor(sample(runif(5), 20, replace = TRUE)) ## [1] 0.0248644019011408 0.0248644019011408 0.179684827337041 ## [4]…
Adam SO
  • 9,821
  • 8
  • 28
  • 27
661
votes
23 answers

Convert Int to String in Swift

I'm trying to work out how to cast an Int into a String in Swift. I figure out a workaround, using NSNumber but I'd love to figure out how to do it all in Swift. let x : Int = 45 let xNSNumber = x as NSNumber let xString : String =…
Steve Marshall
  • 7,919
  • 4
  • 16
  • 17
616
votes
15 answers

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?
kman99
  • 7,315
  • 4
  • 24
  • 20
603
votes
11 answers

When to use reinterpret_cast?

I am little confused with the applicability of reinterpret_cast vs static_cast. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static. This is the cast the C++ compiler…
HeretoLearn
  • 7,124
  • 6
  • 24
  • 22
1
2 3
99 100