> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/math/mathfunctions/roundfloat.md).

# RoundFloat

### Introduction

The RoundFloat method can be used to round a float to the nearest non-whole argument value (called multipleOf). The following shows the result of using RoundFloat with multipleOf = 1: RoundFloat(1.2f) -> 1 RoundFloat(3.8f) -> 4 RoundFloat(-8.3) -> -8 If your multipleOf = 2, you would see the following results: RoundFloat(1.2) -> 2 RoundFloat(3.8f) -> 4 RoundFloat(-8.3) -> -8 This method is very useful in tile-based games where your tiles are not centered on whole values.

### Code Example

The following shows how to use the RoundFloat method: Add the following using statement:

```
using FlatRedBall.Math;
```

Add the following code wherever you need to use RoundFloat:

```
float valueToRound = 2.1f;
// Notice that multipleOf does not have to be a whole number:
float multipleOf = .481f;

float result = MathFunctions.RoundFloat(valueToRound, multipleOf);
// result will equal 1.924, which is .481 * 4.
```

### Code Example: Using RoundFloat for even-spacing index

RoundFloat can be useful for identifying the index of an object based off of its position. For this example, consider a list of Buttons, stacked vertically. The first button appears at Y = 10, and each button is spaced 35 units away from the next. In this situation, you can convert any absolute Y value to the index of the nearest button as follows:

```
const float startingY = 10;
const float spacingBetweenButtons = 35;
// Assume worldY is the Y value that you want to convert to an index
float indexAsFloat = MathFunctions.RoundFloat(worldY, spacingBetweenButtons, startingY);
// Now we can convert this to an int
int index = MathFunctions.RoundToInt(indexAsFloat);
```

### Code Example: Using RoundFloat to Adjust Camera Offsets

Due to rendering issues in DirectX 9, offsetting the camera by a small amount can correct visual artifacts. The following code shows how to adjust the camera so its position is always offset by .1 pixels (meaning, it X position might be 10.1, 11.1, 12.1, etc):

```lang:c#
// Assuming the Camera is not attached to any object,
// and that its position has already been set to the desired values:

// offset by .1 pixel
const float offset = ..1f;
const float roundingValue = 1;

Camera.Main.X = MathFunctions.RoundFloat(Camera.Main.X, roundingValue, offset);
Camera.Main.Y = MathFunctions.RoundFloat(Camera.Main.Y, roundingValue, offset);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/math/mathfunctions/roundfloat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
