Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,140 questions

56,014 answers

573 users

How to convert a decimal to integer in Scala

1 Answer

0 votes
/*
    ============================================================
    Convert a decimal-like value to Long in Scala.

    This program demonstrates:
      • Conversion using Math.round() for rounding.
      • Conversion using toLong for truncation.
      • Conversion using BigDecimal.toLongExact for strict conversion.
      • A helper method that prints all conversion styles.

    Notes:
      • Scala does not have a separate "decimal" type; Double and BigDecimal
        are used for decimal values.
      • Math.round returns a Long.
      • toLong truncates the fractional part.
      • BigDecimal.toLongExact throws if the value is fractional or out of range.
    ============================================================
*/

object DecimalToLongDemo {

  // Rounds a Double to Long
  def convertDoubleToLong(value: Double): Long =
    Math.round(value)

  // Truncates a Double to Long
  def castDoubleToLong(value: Double): Long =
    value.toLong

  // Converts BigDecimal using truncation
  def convertBigDecimalToLong(value: BigDecimal): Long =
    value.toLong

  // Converts BigDecimal strictly (throws if fractional)
  def convertBigDecimalExact(value: BigDecimal): Either[String, Long] =
    try {
      Right(value.toLongExact)
    } catch {
      case _: ArithmeticException =>
        Left("ERROR — fractional or out of range")
    }

  // Prints conversion styles for Double
  def showDoubleConversions(value: Double): Unit = {
    println(s"Input decimal (Double): $value")
    println(s"Rounded (Math.round): ${convertDoubleToLong(value)}")
    println(s"Truncated (toLong): ${castDoubleToLong(value)}")
    println()
  }

  // Prints conversion styles for BigDecimal
  def showBigDecimalConversions(value: BigDecimal): Unit = {
    println(s"Input decimal (BigDecimal): $value")
    println(s"Truncated (toLong): ${convertBigDecimalToLong(value)}")

    convertBigDecimalExact(value) match {
      case Right(v) => println(s"Exact (toLongExact): $v")
      case Left(err) => println(s"Exact (toLongExact): $err")
    }

    println()
  }

  def main(args: Array[String]): Unit = {
    // Double examples
    showDoubleConversions(12.7)
    showDoubleConversions(12.3)
    showDoubleConversions(-5.8)
    showDoubleConversions(42.0)

    // BigDecimal examples
    showBigDecimalConversions(BigDecimal("12.7"))
    showBigDecimalConversions(BigDecimal("42"))
    showBigDecimalConversions(BigDecimal("-5.8"))
  }
}



/*
run:

Input decimal (Double): 12.7
Rounded (Math.round): 13
Truncated (toLong): 12

Input decimal (Double): 12.3
Rounded (Math.round): 12
Truncated (toLong): 12

Input decimal (Double): -5.8
Rounded (Math.round): -6
Truncated (toLong): -5

Input decimal (Double): 42.0
Rounded (Math.round): 42
Truncated (toLong): 42

Input decimal (BigDecimal): 12.7
Truncated (toLong): 12
Exact (toLongExact): ERROR — fractional or out of range

Input decimal (BigDecimal): 42
Truncated (toLong): 42
Exact (toLongExact): 42

Input decimal (BigDecimal): -5.8
Truncated (toLong): -5
Exact (toLongExact): ERROR — fractional or out of range

*/

 



answered Aug 6 by avibootz
...