For a starter

convert number to English words

/**
  * @author lwpro
  * @since 10/17/2017
  * @version 1
  */
object NumberTranslator extends App {

  def translateSingle(num: Int): String = {
    num match {
      case 0 => "zero"
      case 1 => "one"
      case 2 => "two"
      case 3 => "three"
      case 4 => "four"
      case 5 => "five"
      case 6 => "six"
      case 7 => "seven"
      case 8 => "eight"
      case 9 => "nine"
    }
  }

    def translateDouble(num: Int): String = {

      num match {
        case 10 => "ten"
        case 11 => "elven"
        case 12 => "twelve"
        case 13 => "thirteen"
        case 14 => "fourteen"
        case 15 => "fifteen"
        case 16 => "sixteen"
        case 17 => "seventeen"
        case 18 => "eighteen"
        case 19 => "nineteen"
        case 20 => "twenty"
        case x if 21 until 30 contains x => "twenty " concat (translateSingle(x - 20))
        case 30 => "thirty"
        case x if 31 until 40 contains x => "thirty " concat (translateSingle(x - 30))
        case 40 => "forty"
        case x if 41 until 50 contains x => "forty " concat (translateSingle(x - 40))
        case 50 => "fifty"
        case x if 51 until 60 contains x => "fifty " concat (translateSingle(x - 50))
        case 60 => "sixty"
        case x if 61 until 70 contains x => "sixty " concat (translateSingle(x - 60))
        case 70 => "seventy"
        case x if 71 until 80 contains x => "seventy " concat (translateSingle(x - 70))
        case 80 => "eighty"
        case x if 81 until 90 contains x => "eightty " concat (translateSingle(x - 80))
        case 90 => "ninety"
        case x if 90 until 100 contains x => "ninety " concat (translateSingle(x - 90))
      }
    }


    def translateBlock(num: Int) = {
      num match {
        case x if 0 until 10 contains x => translateSingle(num)
        case x if 10 until 100 contains x => translateDouble(num)
        case x if (100 until 1000 contains x) && (x %100 == 0) => translateSingle(num / 100) concat " hundred"
        case x if x % 100 < 10 => translateSingle(num / 100) concat " hundred and " concat (translateSingle(num % 100) )
        case _ => translateSingle(num / 100) concat " hundred and " concat (translateDouble(num % 100) )
      }
    }

  for (i <- 0 until 1000)
    println( i.toString concat("::") concat translateBlock(i))


  def translateWhole (num: Int) = {
    num toString() length  match {
      case x if 0 until 3 contains x => translateBlock(num)
      case x if 4 until 6 contains x => translateBlock(num / 1000) concat("thousand and ") concat(translateBlock(num %1000))
      case x if 7 until 9 contains x => translateBlock(num / 1000000) concat("million and ") concat translateBlock(num % 1000 /1000) concat("thousand and ") concat(translateBlock(num %1000 /1000 % 1000))
    }
  }

  }


Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s