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
145
votes
8 answers

Converting BigDecimal to Integer

I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods. Now how to convert the…
Vishal
  • 2,711
  • 7
  • 33
  • 42
143
votes
3 answers

Converting double to integer in Java

In Java, I want to convert a double to an integer, I know if you do this: double x = 1.5; int y = (int)x; you get y=1. If you do this: int y = (int)Math.round(x); You'll likely get 2. However, I am wondering: since double representations of…
vdMandele
  • 1,629
  • 2
  • 11
  • 8
142
votes
5 answers

How to avoid warning when introducing NAs by coercion

I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using as.numeric to convert a character vector. For example: x <- as.numeric(c("1", "2", "X")) Will give me a warning because it…
Corvus
  • 7,548
  • 9
  • 42
  • 68
141
votes
17 answers

Converting Integer to Long

I need to get the value of a field using reflection. It so happens that I am not always sure what the datatype of the field is. For that, and to avoid some code duplication I have created the following method: @SuppressWarnings("unchecked") private…
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
141
votes
16 answers

Swift double to string

Before I updated xCode 6, I had no problems casting a double to a string but now it gives me an error var a: Double = 1.5 var b: String = String(a) It gives me the error message "double is not convertible to string". Is there any other way to do…
tim_yng
  • 2,591
  • 4
  • 18
  • 20
141
votes
4 answers

Passing shared_ptr as shared_ptr

What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptrs by reference to avoid a needless copy: int foo(const shared_ptr& ptr); but this…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
140
votes
3 answers

Import pandas dataframe column as string not int

I would like to import the following csv as strings not as int64. Pandas read_csv automatically converts it to int64, but I need this column as…
Oliver
  • 1,785
  • 3
  • 13
  • 18
139
votes
5 answers

Can I assume (bool)true == (int)1 for any C++ compiler?

Can I assume (bool)true == (int)1 for any C++ compiler ?
Petruza
  • 11,744
  • 25
  • 84
  • 136
138
votes
4 answers

Double colon `::` notation in SQL

I have picked up someone's code, the following is a part of a WHERE clause. What does the double colon indicate here? b.date_completed > a.dc::date + INTERVAL '1 DAY 7:20:00'
Pat
  • 1,381
  • 2
  • 8
  • 3
138
votes
9 answers

Build a function object with properties in TypeScript

I want to create a function object, which also has some properties held on it. For example in JavaScript I would do: var f = function() { } f.someValue = 3; Now in TypeScript I can describe the type of this as: var f: { (): any; someValue: number;…
JL235
  • 2,455
  • 2
  • 19
  • 14
137
votes
17 answers

SQL error "ORA-01722: invalid number"

A very easy one for someone, The following insert is giving me the ORA-01722: invalid number why? INSERT INTO CUSTOMER VALUES (1,'MALADY','Claire','27 Smith St Caulfield','0419 853 694'); INSERT INTO CUSTOMER VALUES (2,'GIBSON','Jake','27 Smith…
Phillip Gibson
  • 1,485
  • 2
  • 10
  • 5
136
votes
5 answers

Cast a Double Variable to Decimal

How does one cast a double to decimal which is used when doing currency development. Where does the M go? decimal dtot = (decimal)(doubleTotal);
flo
  • 1,471
  • 2
  • 12
  • 13