tags:

views:

696

answers:

4

I have a dynamically changing input reading from a file. The numbers are either Int or Double. Why does Scala prints .0 after every Double numbers? Is there a way for Scala to print it the same way it reads it

Example:

var x:Double = 1
println (x)             // This prints '1.0', I want it to print '1'
x = 1.0                 // This prints '1.0', which is good

I can't use Int because some of the input I get are Doubles. I can't use String or AnyVal because I perform some math operations.

Thank you,

+3  A: 

Use printf:

printf("The value is %.0f", x)

For a description of the format string, see this page from the Java SE 6 API documentation.

Note that you can ofcourse also use the Java library from Scala, so other ways to format numbers from Java can also be used from Scala. You can for example use class java.text.DecimalFormat:

val df = new java.text.DecimalFormat("#####")
println(df.format(x))
Jesper
Thanks for you replyBut the problem is that the number is unknown, either it is '1' or '1.0'. If it '1' I want to print '1', if it is '1.0' then I want to print '1.0'. But since it is Double, Scala always prints is as '1.0'
If you want that, you'll need to store it as a string. A `double` doesn't remember whether or not you assigned it with `1` or `1.0`.
cdmckay
Kodo, numbers do not inherently have a number of digits. There is no difference between a number that has the value 1 and a number that has the value 1.0.
Jesper
@Jesper, you are correct, they have same value. The only problem is during printing and displaying it to human. If they put '1 + 1' they expect '2' not '2.0'
@Kodo Use BigDecimal
Jorge Ortiz
+6  A: 
var x:Double = 1
var y:Double = 1.0

print(x) // => 1.0
print(y) // => 1.0

If i understand you question you want scala to print x and y differently? The problem is that x and y are both a variable of the type Double and look the same.

Why do you explicitly define the type of the vars?

var x = 1 
var y= 1.0

print(x) // => 1
print(y) // => 1.0
nuriaion
+1  A: 

Use type inference, rather than explicit typing.

scala> val xi = 1
xi: Int = 1

scala> val xd = 1.0
xd: Double = 1.0

scala> println(xi)
1

scala> println(xd)
1.0
Synesso
Ooops sorry this is a duplicate answer.
Synesso
+3  A: 

scala> "%1.0f" format 1.0 res3: String = 1

If your input is either Int or Double, you can do it like this:

def fmt(v: Any): String = v match {
  case d : Double => "%1.0f" format d
  case i : Int => i.toString
  case _ => throw new IllegalArgumentException
}

Usage:

scala> fmt(1.0)
res6: String = 1

scala> fmt(1)
res7: String = 1

scala> fmt(1.0f)
java.lang.IllegalArgumentException
        at .fmt(<console>:7)
        at .<init>(<console>:6)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:4)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.Dele...

Otherwise, you might use BigDecimals. They are slow, but they do come with the scale, so "1", "1.0" and "1.00" are all different:

scala> var x = BigDecimal("1.0")
x: BigDecimal = 1.0

scala> x = 1
x: BigDecimal = 1

scala> x = 1.0
x: BigDecimal = 1.0

scala> x = 1.000
x: BigDecimal = 1.0

scala> x = "1.000"
x: BigDecimal = 1.000
Daniel