Skip to content

Instantly share code, notes, and snippets.

@trackd
Created June 14, 2025 21:25
Show Gist options
  • Select an option

  • Save trackd/aceb795b95dc98b07feb62858cd9cb34 to your computer and use it in GitHub Desktop.

Select an option

Save trackd/aceb795b95dc98b07feb62858cd9cb34 to your computer and use it in GitHub Desktop.
function ConvertTo-DollarWords {
<#
.EXAMPLE
ConvertTo-DollarWords '$1234.56'
ConvertTo-DollarWords '$1234,56'
ConvertTo-DollarWords '$1.01'
#>
param(
[string] $Amount
)
$Amount = $Amount -replace '[^\d.,]'
$number = [double]::MinValue
$culture = $Amount.Contains(',') ? [CultureInfo]::CurrentCulture : [CultureInfo]::InvariantCulture
if (-not [double]::TryParse($Amount, [Globalization.NumberStyles]::Any, $culture, [ref]$number)) {
throw "Invalid amount: $Amount"
}
$dollarNumber = [math]::Floor($number)
$centNumber = [math]::Round(($number - [math]::Floor($number)) * 100)
@(
if ($dollarNumber -gt 0) {
$dollarWord = [Humanizer.NumberToWordsExtension]::ToWords($dollarNumber, $false)
$unit = $dollarNumber -eq 1 ? 'dollar' : 'dollars'
"$dollarWord $unit"
}
if ($centNumber -gt 0) {
$centWord = [Humanizer.NumberToWordsExtension]::ToWords($centNumber, $false)
$unit = $centNumber -eq 1 ? 'cent' : 'cents'
"$centWord $unit"
}
) -join ' & '
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment