> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/rounding-numbers-with-precision.md).

# Rounding Numbers With Precision

Ruby's `Float#round` method gets the job done, but doesn't offer much configurability. If you'd like to finely control how a rounded number will display, `ActiveSupport::NumberHelper` offers [`number_to_rounded`](https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_rounded).

When a precision is specified, it will apply to the fraction digits:

```ruby
> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2)
=> "1.00"
```

Unless you include `significant: true` in which case precision will refer to the number of signficant digits:

```ruby
> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2, significant: true)
=> "1.0"
```

Because this is for display purposes, the return value is a string. You can further specify that insignificant zeros are stripped from the result:

```ruby
> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2, significant: true, strip_insignificant_zeros: true)
=> "1"
```

And for completeness, here is an example of a number being rounded up:

```ruby
> ActiveSupport::NumberHelper.number_to_rounded(1.29, precision: 2, significant: true)
=> "1.3"
```

[source](https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_rounded)


---

# 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:

```
GET https://ploegert.gitbook.io/til/programmy/rails/rounding-numbers-with-precision.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
