Good day! There is a variable $ a = 37.5 (with a comma) in powershell . How to round it up to integers? I tried this:

[math]::Round($a, 0) 

An error is displayed:

 Не удается найти перегрузку для "Round" и количества аргументов: "2". строка:1 знак:1 + [math]::Round($a, 0) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest 

When I enter the value itself, instead of the variable

 [math]::Round(37,5, 0) 

then everything works:

 37 
  • 2
    [math]::Round(37,5, 0) - Here you have 3 parameters. - vp_arth
  • This is exactly how 3 parameters are used, not 2 from the author, so it seems to him that everything works as he expects. static double Round (double value, int digits, System.MidpointRounding mode) - Kirill Pashkov

4 answers 4

The problem is most likely that the current configuration is awaiting. (point) as the separator of the fractional part, and declaring a variable through $ a = 37.5 creates an array with two numbers, 37 and 5, which causes this error.

(Get-Culture).NumberFormat will show the current settings for fractional separators.

The problem in this case is not that the variable contains a number with a comma as a separator. The variable is initially initialized as an array:

 $a = 37,5 PS C:\> $a.GetType().BaseType.Name Array PS C:\> $a.Count 2 

Also, the separator symbol for the integer and fractional parts is determined by the current regional settings of the system (session). But you have to set the value through a period, even if you have a comma. Otherwise it will be perceived as an array.

  • here's how to round a value with a comma? - mocart
  • @mocart change the regional settings on the host or give the variable value in $ a = '37, 5'.Replace (',', '.') - Kirill Pashkov
  • The value must remain a number, not a string. and you need to do it somehow without replacing regional settings - mocart
  • The problem is not a semicolon. When creating a variable with the current regional settings, the variable will always contain two numbers, 37 and 5. check. - Kirill Pashkov

PowerShell displays delimited numbers by region, we all have this obviously comma. But you must always enter the numbers yourself in American with a dot. Because a comma is an "array creation operator". When you enter 3.14 this is the number, "3.14" or "3,14" is the string. It is easier to round to integer like this: [int]3.14

To any character: [math]::Round(3.1416, 2)

    1. You can use the metd with 1m parameter :.

      PS C:> $ a = 22/7

      PS C:> $ a

      3.14285714285714

      PS C:> [math] :: Round ($ a)

      3

    2. You must set the value through a dot, even if you have a comma in the regional settings. Otherwise it will be perceived as an array.

    $ a = 37.5

    This is done so that you could specify, regardless of the regional settings with the same syntax, the values, both in Arrey (14.4, 14.5, 1.4) and directly (13.4), because if it affected you, it would cause problems with (14.4, 14.5, 1.4)

    • $ a = 37.5 - because of the regional settings (and maybe even because of what) there is a comma, however, this variable is recognized as an array. Try replacing 3.14285714285714 with 3.14285714285714 in your answer - and it will not work :( - labris
    • But you have to set the value through a period, even if you have a comma in the regional settings. Otherwise it will be perceived as an array. - Andrew

    At first I tried to format the number - but I forgot that in this case (when all the digits after the comma are discarded) rounding will always go only downwards.

    I think we managed to find the shortest and most efficient solution. Alas, if you get a zero after the decimal point, it does not work. However, I'll leave it here for now.

    If the string is recognized as a numeric value, then it will be considered a number (with some exceptions, which in this case do not interfere with us).

     $a = 37,5 $ofs = '.' $a = [string]$a $a = [math]::Round($a) $a 

    PS It does not work for numbers (although in fact it turns out not a number, but an array) of the form 3,0007 - converts to 3.7 and rounds to 4.

    • $a = 37,00005 - PetSerAl
    • Screenshot can be in the studio? my here: yadi.sk/i/mhFk4yX43FXJ8p - labris
    • Indeed, 37.00005 is reduced to 37.5 .... We must try it differently - labris
    • '37, 0005'.Replace (',', '.') - it works, but how to convert $ a into a string - until I get it, the zeros disappear into $ a = [string] $ a :) - labris