Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cast Integer to String in Scala?

Tags:

casting

scala

I searched for a while the answer to this question but came out empty. What is the simple command of casting variable X which is Integer, to a String?

like image 738
izac89 Avatar asked Jun 01 '13 15:06

izac89


People also ask

Can you cast an int into a string?

We can convert int to String in java using String. valueOf() and Integer. toString() methods.

What is toString in Scala?

The toString() method is utilized to return the string representation of the specified value. Method Definition: def toString(): String. Return Type: It returns the string representation of the specified value. Example #1: // Scala program of Int toString()

What is some () in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.


2 Answers

If you have variable x of type Int, you can call toString on it to get its string representation.

val x = 42 x.toString // gives "42" 

That gives you the string. Of course, you can use toString on any Scala "thing"--I'm avoiding the loaded object word.

like image 89
janm399 Avatar answered Oct 31 '22 05:10

janm399


Is it simple enough?

scala> val foo = 1 foo: Int = 1  scala> foo.toString res0: String = 1  scala> val bar: java.lang.Integer = 2 bar: Integer = 2  scala> bar.toString res1: String = 2 
like image 30
om-nom-nom Avatar answered Oct 31 '22 06:10

om-nom-nom