Prepare for the Blockchain Developer Certification. Master blockchain concepts with flashcards and multiple choice questions. Explanation-based learning for your success!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


In Solidity, what type would you use to define a variable that can hold decimal values?

  1. uint

  2. int

  3. fixed/ufixed

  4. string

The correct answer is: fixed/ufixed

In Solidity, the appropriate type for defining a variable that can hold decimal values is `fixed` and `ufixed`. These are specifically designed to handle fractional numbers. The `fixed` type allows for signed decimal values, while `ufixed` is for unsigned decimal values. The `fixed` and `ufixed` types accommodate real numbers with a defined precision, enabling developers to work with values that are not whole numbers, essential in scenarios like financial calculations where precision matters. Other types provided in the options serve different purposes. The `uint` and `int` types are used for whole numbers, either non-negative (uint) or signed integers (int), which means they cannot represent decimal points. The `string` type is used for text and does not relate to numeric values, further confirming that it is unsuitable for holding decimal values. Therefore, using `fixed` or `ufixed` is essential for handling calculations with decimals effectively in a Solidity smart contract, as they ensure the integrity of numerical data that requires fractional representation.