fcvt - Linux


Overview

fcvt is a Linux command that converts a floating-point number to its decimal string representation. It is commonly used to format floating-point values for output.

Syntax

fcvt [-a] [-ed] [-f fmt] [-l] [-p prec] [-s style] num

Options/Flags

  • -a: Use American-style currency formatting.
  • -e: Use exponential notation.
  • -d: Use fixed-point notation with decimal point.
  • -f fmt: Use custom format string fmt.
  • -l: Use locale-specific formatting.
  • -p prec: Set precision to prec significant digits.
  • -s style: Set style to fixed, exp, or gen.

Examples

  • Convert 1.23 to a string:
fcvt 1.23
  • Convert 123.45 to a currency string:
fcvt -a 123.45
  • Convert 1.23e5 to fixed-point notation with 2 digits precision:
fcvt -d -p 2 1.23e5
  • Convert 1.23 to exponential notation using locale-specific formatting:
fcvt -e -l 1.23

Common Issues

  • Incorrect format: Ensure the correct format flags (-e, -d, -f) are used based on the desired output.
  • Invalid precision: The precision (-p) must be a positive integer.
  • Non-numeric input: The input to fcvt must be a valid floating-point number.

Integration

fcvt can be integrated with other commands for advanced tasks:

  • Print current system time in currency format:
date +%T | fcvt -a
  • Convert a list of numbers to a currency-formatted table:
echo "1.23,4.56,7.89" | tr ',' '\n' | fcvt -a

Related Commands

  • printf: Formatted output using a format string.
  • bc: Arbitrary-precision calculator.