Related Tags

scala
number
communitycreator

# How to print the hexadecimal value of a number in Scala

Theodore Kelechukwu Onyejiaku

## Overview

Hexadecimal numbers refer to numbers that are in base 16. Standard numbers are characterized in decimals, that is, base 10. However, in Scala, we can present this decimal value in hexadecimal format. We need to use the function printf() and the corresponding format %X .

### Syntax

printf("%X", decimalNumber)
Syntax for printing a decimal number in hexadecimal in Scala

### Parameters

%X: We use this format to tell the printf() function to print the number value decimalNumber in hexadecimal format.

decimalNumber: This is the number we want to print in hexadecimal format.

### Return value

The number value decimalNumber is returned as a hexadecimal value.

### Example

object Main extends App {
//  create some decimal numbers
var num1 = 90
var num2 = 10
var num3 = 4
var num4 = 1000

// print the decimals in hexadecimals
printf("%d in hexadecimal is %X", num1, num1)
printf("\n")
printf("%d in hexadecimal is %X", num2, num2)
printf("\n")
printf("%d in hexadecimal is %X", num3, num3)
printf("\n")
printf("%d in hexadecimal is %X", num4, num4)
}
Print decimal numbers in Hexadecimal format

### Explanation

• line 3–6: We create some decimal variables and instantiate them.
• line 9–15: We print the decimal variables in decimal formats using %d and then in hexadecimal using %X.

RELATED TAGS

scala
number
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time