Skip to main content

percent.go

percent.go - Overview

  1. Overview This file defines a percentFormatter for formatting floating-point numbers as percentages. It includes functions to format values with and without multiplying by 100, appending a "%" symbol.

  2. Detailed Documentation

percentFormatter

  • Purpose: percentFormatter is a struct that implements the Formatter interface for formatting numbers as percentages.
  • Parameters: None
  • Returns: None

NewPercentFormatter

  • Purpose: Creates a new instance of percentFormatter.
  • Parameters: None
  • Returns: Formatter: A pointer to a new percentFormatter instance.

(*percentFormatter) Name

  • Purpose: Returns the name of the formatter, which is "percent".
  • Parameters: None
  • Returns: string: "percent"

toPercent

  • Purpose: Formats a floating-point number to a percentage string with a specified number of decimal places, appending a "%" symbol.
  • Parameters:
    • value (float64): The value to format.
    • decimals (DecimalCount): The number of decimal places to include (currently unused).
  • Returns: string: The formatted percentage string.

toPercentUnit

  • Purpose: Multiplies a floating-point number by 100 and formats it to a percentage string with a specified number of decimal places, appending a "%" symbol.
  • Parameters:
    • value (float64): The value to format.
    • decimals (DecimalCount): The number of decimal places to include (currently unused).
  • Returns: string: The formatted percentage string.

(f *percentFormatter) Format

  • Purpose: Formats a floating-point number to a percentage string based on the specified unit.
  • Parameters:
    • value (float64): The value to format.
    • unit (string): The unit of the value. If the unit is "percent", the value is formatted as a percentage directly. If the unit is "percentunit", the value is multiplied by 100 before formatting as a percentage.
  • Returns: string: The formatted percentage string. If the unit does not match "percent" or "percentunit", the function returns the value formatted as a string using fmt.Sprintf.
  1. Code Examples N/A

  2. Clarity and Accuracy The documentation reflects the code accurately without additional assumptions.

  3. Markdown & MDX Perfection The markdown is valid and should not cause issues.

  4. Edge Cases To Avoid Breaking MDX All potential MDX breaking characters are escaped or handled correctly.

  5. Getting Started Relevance Include in Getting Started: NO