STWilson
STWilson

Reputation: 1718

set negative number to zero Powershell

With Powershell, is there a function to set a negative number to zero?

Or is this it?

If ($num -lt 0) {$num = 0}

Upvotes: 3

Views: 1479

Answers (1)

mklement0
mklement0

Reputation: 439228

In case you're looking for a more concise way:

$num = [math]::max(0, $num)

A PowerShell (Core) 7+ alternative, using the ternary conditional operator:

$num = $num -lt 0 ? 0 : $num

Upvotes: 12

Related Questions