![]() ![]() I invite you to follow me on Twitter and Facebook. Numbers Week will continue tomorrow when I will talk about more cool stuff. PA, that is all there is to using custom format strings to control the display of decimal places in Windows PowerShell. Select-Object script and its associated output are shown here: In the following image, I see the CPU time of several process reports as 0.03, and 0.02.Īs shown here, if I format the output so that it only displays a single decimal place, I lose much of the CPU output: I can do this with Select-Object, Format-Table, or Format-List (and even with Out-GridView). If I am working with output from a Windows PowerShell cmdlet, I can use the same technique by using a customized property. The number 1.1 displays as 1.1, even though my format string of “#.#” contains two pound signs. In the previous output, 5.55555 rounds up to 5.56, but 4.4444 rounds down to 4.44. The following example illustrates how I limit my output to two decimal places: On the left side of the decimal, everything becomes rounded up if there are additional numbers, or nothing happens if there are no numbers. If I have numbers on the right side of the decimal, I need only a single pound sign. So in the example, I had two decimal places, but I only wanted one. Here is an example of changing a number that has two decimal places to a number that contains a single decimal place:Īll I need to do is use the pound sing ( # ) where I want a digit to appear. The great thing is that I can easily override the method with my own custom format string. The ToString method is everywhere in Windows PowerShell-even number types contain a ToString method. To do this, I use a custom number format string. So if I am trying to gain a quick impression, I like to easily change to one or two decimal places. However, when I am trying to read data, I have a problem keeping track of more than two or three decimal places. So as a best practice, put it off and make these types of conversions the last thing you do to your data. This is because I can easily create a report from the database that displays only one decimal place, but I have no idea what the second decimal place might be.Īdditionally, I could run into other problems trying to perform math operations on strings instead of numbers. But if I am storing the data, perhaps in a database, it is probably best to store the actual number. This change might be OK if I am sending the output to its final destination-for example, displaying it to the Windows PowerShell console or printing it on a printer. In the following example, I convert from a Double to a String: When I use a custom format string to control the number of decimal places, I change the underlying type from a number type to a string. Using a custom format string to control decimal places I like to know where I am going, but then I hold off until just when I need to do something before I actually do it. Well, PA, a similar thing happens when I am working with Windows PowerShell. I mean, that is what the planning stage is for, right? To plan? But when I have a plan in place, I do not give it a lot of extra thought. ![]() I need to know what I am going to be doing a week from now, a month from now, and even a year from now. Like a lot of people, I like to work with a plan. ![]() Microsoft Scripting Guy, Ed Wilson, is here. I would love to see how a real pro does it. I have also tried using Trim or other string techniques with mixed success. I mean, how do I really get rid of them-at least properly? I have done things like convert them to integers, but that loses me all of the decimal places. Hey, Scripting Guy! One of the things I have a problem with is when I get numbers that have a lot of decimal places. Summary : Microsoft Scripting Guy, Ed Wilson, talks about using strings to format the display of decimal places in Windows PowerShell. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |