# a hacky way I just thought of that happens to work in this scenario $pctValue = ($yourString -split ',' -replace '"') Once you have isolated the number, then you can use my advice above: $yourString = '"Percentage of records ","0.02"' In the event it is a single string, then you need to extract the number to use it. I assumed it was two strings, which you separated with a comma. I hadn't considered that your sample may be a single string, like: Note: The percentage format string will round your value to the nearest decimal that you specify.Īs pointed out in the comments. In your case, you want to show zero decimal places. 0 - controls the number of decimal places to show. P - means to format the value as a percentage, this performs the N * 100 operation for you and then adds on the percent sign ToString()īut we want to format it as a percentage, so we supply P0 as a parameter. If you do need the context of a specific culture, pass a instance ( ) instead of $null e.g., to parse in the context of the French (France) culture, pass 'fr-FR'.Yes, you can convert the string to a float data type (single, double, decimal), and then convert it back using a format string, like so: "Percentage of records ", ('0.02').ToString('P0')Īnd if you want it to output in a single line, you could use: "Percentage of records: $(('0.02').ToString('P0'))"Ĭonvert your string to a float datatype: '0.02'Ĭonvert that float back into a string. use embedded quoting, and given that only digits are involved, not (culture-dependent) names, such as month names. Note that, due to use of $null, the current culture is used for parsing, which has no effect in this case, however, given that the separator chars. ToString() as described above: ::ParseExact( If your original string representation were not recognized in the invariant culture, you can use to parse your input string into a ( System.DateTime) instance before calling. This is necessary, because characters such as / are by default interpreted as placeholders for the culture-appropriate date separator, for instance, so - depending on the culture in effect (as reflected in $PSCulture) - they may be translated to a different character, such as. and / are escaped / quoted so as to indicate that they're to be used verbatim (you're free to choose between \-escaping and embedded quoting). The following should work irrespective of the specific culture in effect: PS> ( '').ToString('MM\-dd\-yyyy') ToString() with the desired format string to produce the desired string format, as shown in the many answers to this question. cast your string to to create a System.DateTime instance.Since PowerShell's casts use the invariant culture and format yyyy-MM-dd (as exemplified by your sample input string, '') happens to be a recognized format in that culture, you can simply: and / should be escaped for maximum robustness. See below for a truly culture-invariant solution, which also explains why separator chars. PowerShell generally tries to use the invariant culture in order for code to work the same across cultures, but in the case of cmdlet arguments actually is culture-sensitive, which is a historical accident that cannot be fixed without breaking backward compatibility. This will work with most cultures in effect, because format yyyy-MM-dd (as exemplified by your sample input string, '') is a recognized format in all but a few cultures. PS> Get-Date -Date -Format dd\-MM\-yyyy # or dd\/MM\/yyyy Note: This question is a near-duplicate of Change date format from "yyyymmdd" to "mm/dd/yyyy" while the two are closely related, this question is more overtly focused on both parsing from a string to a date and then back to a string, using a different format by contrast, the linked question has the from aspect covered as an incidental part of the question.īali C, in a comment on the question, came up with the most concise solution, which only requires a small tweak: # Note the "\"-escaped separators - see below.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |