rust-lang / rust-lang/rfcs

Explicit float and integer formatting

Open
#2,042 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently, formatting all floating-point values uses the shortest possible representation by default (as implemented in /src/libcore/fmt/float.rs. This is implemented such that floating-point values with no fractional parts are displayed as integers, e.g. 13.0f64 becomes formatted as "13". This also means that the only way to currently always achieve a formatting that always displays the decimal point is to give a specific precision.

So as not to introduce any incompatibilities, I'd suggest introducing two new format types and traits associated with them as such:

  • d => Decimal
  • f => Fractional

The Decimal trait should rounded to the nearest integer of the value and represent it as an integer without any decimal point.
The Fractional trait should always display the value with a decimal point. If no precision is given, it produces the fewest digits after the decimal point needed to display the exact value. If precision is given, it behaves the same as it currently does without any specified format type.

This can be technically achieved with relative ease for the f32 and f64 types as the float_to_decimal_common function in /src/libcore/fmt/float.rs can be changed to accept a minimum decimal point parameter which can be passed to the call to to_shortest_str to be used for the Fractional trait.

For the Decimal trait, the float_to_decimal_common function can be modified further to accept an argument to display as integer with no fractional part.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the proposal and the referenced src/libcore/fmt/float.rs, starting with float_to_decimal_common and to_shortest_str. Trace how existing floating-point formatting handles precision and format types. Done means the proposed Decimal and Fractional behaviors are defined without breaking existing formatting, with the f32 and f64 paths covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.