GithubHelp home page GithubHelp logo

vi / float-pretty-print Goto Github PK

View Code? Open in Web Editor NEW
10.0 5.0 0.0 49 KB

Format f64 in Rust with minimum and maximum width, auto switching between scientific and normal representation

Perl 0.87% Rust 99.13%
rust format human-readable float f64 print string pretty-print

float-pretty-print's Introduction

float-pretty-print

Round and format f64 number for showing it to humans, with configurable minimum and maximum width. It automatically switches between exponential and usual forms as it sees fit.

It works by trying usual format!, possibly multiple times and inspecting the resulting string.

Only two formatting parameters are supported:

  • width is minimum width
  • precision is maximum width

### is printed if it can't output the number with given constraint.

Example:

  use float_pretty_print::PrettyPrintFloat;
  assert_eq!(format!("{}", PrettyPrintFloat(3.45)), "3.45");
  assert_eq!(format!("{}", PrettyPrintFloat(12.0)), "12.0");
  assert_eq!(format!("{}", PrettyPrintFloat(120000000.0)), "1.2e8");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0)), "12345");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12.345)), "12.35");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(0.12345)), "0.123");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(1234500000.0)), "1.2e9");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-19)), "1e-15");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-100)), "1e-96");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-130)), "    0");
  assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e+130)), "1e134");
  assert_eq!(format!("{:4.4}", PrettyPrintFloat(12345.0e+130)), "####");
  assert_eq!(format!("{:6.6}", PrettyPrintFloat(12345.0e-130)), "1e-126");

Supports even Rust 1.23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.