Unix / Linux: Bash Number Currency Formatting Thousands Grouping Separator

Unix / Linux: Bash Number Currency Formatting Thousands Grouping Separator

To format a number with a currency thousands grouping separator in Bash on a Unix or Linux system, you can use the awk command.

The awk command allows you to perform advanced text processing and manipulation, including formatting numbers with a currency thousands grouping separator.

Here is an example of how you can use the awk command to format a number with a currency thousands grouping separator:

refer‮:ot ‬lautturi.com
# Define the number
number=12345678

# Format the number with a currency thousands grouping separator
formatted_number=$(awk 'BEGIN { printf "%.2f", '$number'/1000 }')

# Print the formatted number
echo "Formatted number: $formatted_number"

This will output the formatted number in the format "12,345.68".

You can also use the awk command to specify a different thousands grouping separator, such as a period (.) or a space. For example, to use a period as the thousands grouping separator, you can use the following command:

# Format the number with a period as the thousands grouping separator
formatted_number=$(awk 'BEGIN { printf "%.2f", '$number'/1000; OFS="." }')

This will output the formatted number in the format "12.345.68".

Overall, the awk command is a useful tool for formatting numbers with a currency thousands grouping separator in Bash on a Unix or Linux system. It allows you to easily customize the format of numbers and perform advanced text processing tasks.

Created Time:2017-10-30 14:27:29  Author:lautturi