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.

18049 questions
3728
votes
11 answers

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

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…
3463
votes
32 answers

How can I cast int to enum?

How can an int be cast to an enum in C#?
lomaxx
  • 104,787
  • 56
  • 140
  • 177
2703
votes
9 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 C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases?
e.James
  • 109,080
  • 38
  • 170
  • 208
2554
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(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) * length); Why would this be the…
Patrick McDonald
  • 59,808
  • 14
  • 95
  • 115
2028
votes
29 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
  • 23,608
  • 13
  • 48
  • 66
1848
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
  • 51,937
  • 20
  • 74
  • 119
1751
votes
35 answers

How do I check if a string is a number (float)?

What is the best possible way to check if a string can be represented as a number in Python? The function I currently have right now is: def is_number(s): try: float(s) return True except ValueError: return…
Daniel Goldberg
  • 18,355
  • 4
  • 19
  • 28
1037
votes
10 answers

Change column type in pandas

I want to convert a table, represented as a list of lists, into a Pandas DataFrame. As an extremely simplified example: a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']] df = pd.DataFrame(a) What is the best way to convert the columns…
user1642513
767
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
  • 10,014
  • 8
  • 30
  • 48
754
votes
30 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
  • 12,140
  • 12
  • 32
  • 49
708
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
  • 18,568
  • 14
  • 43
  • 56
647
votes
11 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,071
  • 7
  • 24
  • 27
592
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,149
  • 4
  • 13
  • 17
545
votes
14 answers

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?
kman99
  • 6,565
  • 4
  • 21
  • 20
510
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
  • 13,690
  • 3
  • 34
  • 45
1
2 3
99 100