---
title: Validated Numbers
---

All primitive number types have validation, which:
* Controls the number type: bytes stay bytes etc.
* Define a minimum and maximum value.
* Control secondary attributes like which widget the user will see in-GUI when modifying the setting.

### ValidatedNumber
Number validation is defined using one of the six subclasses of `ValidatedNumber`. By default, the allowable min and max will be the entire range of the type (Integer.MIN_VALUE to Integer.MAX_VALUE, for example).

<CodeTabs>

```java !!tabs Java
public float myValidatedFloat = 0.5f; // this value is backed by automatic validation, with no max or min bound
public ValidatedFloat myValidatedFloat = new ValidatedFloat(0.5f, 1f, 0f); // default value, max value, min value
```

```kotlin !!tabs Kotlin
var mySimpleFloat = 0.5f // this value is backed by automatic validation, with no max or min bound
var myValidatedFloat = ValidatedFloat(0.5f, 1f, 0f) // default value, max value, min value
```

</CodeTabs>

### Annotations
Validated Numbers each have their own partnered Annotation you can use to annotate a plain field.

<CodeTabs>

```java !!tabs Java
@ValidatedFloat.Restrict(min = 0f, max = 1f) // the previously unbounded simple float now has automatic validation with bounds between 0 and 1.
public float myValidatedFloat = 0.5f;

@ValidatedInt.Restrict(min = 0, type = ValidatedNumber.WidgetType.TEXTBOX_WITH_BUTTONS) //Restrict can also set the widget type used in the config GUI
public int myWidgetTypeInt = 1;
```

```kotlin !!tabs Kotlin
@ValidatedFloat.Restrict(0f, 1f) // the previously unbounded simple float now has automatic validation with bounds between 0 and 1.
var myValidatedFloat = 0.5f

@ValidatedInt.Restrict(min = 0, type = ValidatedNumber.WidgetType.TEXTBOX_WITH_BUTTONS) //Restrict can also set the widget type used in the config GUI
var myWidgetTypeInt: Int = 1
```

</CodeTabs>

### Format Features

<Callout>
Added in Fzzy Config 0.7.2
</Callout>

There are pass through static extension functions you can use to customize how your number's widget is visually formatted.

<CodeTabs>

```java !!tabs Java
//Provide a custom number text formatter to suit your needs
public ValidatedFloat myValidatedFloat = ValidatedNumber.setFormat(new ValidatedFloat(0.5f, 1f, 0f), new DecimalFormat("#.###"));

//Customize the increment your validation's sliders or increment/decrement buttons use
public ValidatedFloat myValidatedFloat = ValidatedNumber.withIncrement(new ValidatedFloat(0.5f, 1f, 0f), 0.05f);
```

```kotlin !!tabs Kotlin
//Provide a custom number text formatter to suit your needs
var myValidatedFloat = ValidatedFloat(0.5f, 1f, 0f).setFormat(DecimalFormat("#.###"))

//Customize the increment your validation's sliders or increment/decrement buttons use
var myValidatedFloat = ValidatedFloat(0.5f, 1f, 0f).withIncrement(0.05f)
```

</CodeTabs>

### Shorthands
Fzzy Config has shorthand constructors for validated numbers. These are generally used to provide validators for other validation like Lists or Maps, where you may not have any need for restrictions.

<CodeTabs>

```java !!tabs Java
public ValidatedFloat myValidatedFloat = new ValidatedFloat();
```

```kotlin !!tabs Kotlin
var myValidatedFloat = ValidatedFloat()
```

</CodeTabs>

<Callout>
See the documentation pages for <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-int/index.html">Integer 🗗</a>, <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-float/index.html">Float 🗗</a>, <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-double/index.html">Double 🗗</a>, <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-long/index.html">Long 🗗</a>, <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-short/index.html">Short 🗗</a>, <a target="_blank" rel="noopener" href="https://fzzyhmstrs.github.io/fconfig/-fzzy%20-config/me.fzzyhmstrs.fzzy_config.validation.number/-validated-byte/index.html">Byte 🗗</a>
</Callout>
