Skip to main content

DecimalFormat

Struct DecimalFormat 

Source
pub struct DecimalFormat { /* private fields */ }
Expand description

This struct allows you to print a specific number of digits before the decimal point, and after the decimal point.

This exists because the base format trait allows you to specify a width and a precision. However, in a fractional number, the width applies to the WHOLE number, including the fractional component, and doesn’t zero-pad effectively.

  • The first parameter is the number of zero-padded digits before the decimal point.
  • The second parameter is the number of zero-padded digits after the decimal point.

§Example:

use irox_tools::fmt::DecimalFormat;
let fmt = DecimalFormat::new(2,4);

assert_eq!("00.1235", fmt.format_f64(0.1234567));

Implementations§

Source§

impl DecimalFormat

Source

pub fn new(width: usize, precision: usize) -> Self

Source

pub fn format_f64(&self, val: f64) -> String

Formats the specified f64 using this formatter.

§Example:
use irox_tools::fmt::DecimalFormat;
let fmt = DecimalFormat::new(2,4);

assert_eq!("00.1235", fmt.format_f64(0.1234567));
Source

pub fn format_f32(&self, val: f32) -> String

Formats the specified f32 using this formatter.

§Example:
use irox_tools::fmt::DecimalFormat;
let fmt = DecimalFormat::new(2,4);

assert_eq!("00.1235", fmt.format_f32(0.1234567));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> MaybeInto<U> for T
where U: MaybeFrom<T>,

Source§

fn maybe_into(self) -> Option<U>

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.