Trying to export current temperature from the XML to a text file. The results are are "xx.y" but I only need xx exported to the text file. I've tried several commands but keep striking out. Any ideas?
([xml](Invoke-WebRequest -URI http://w1.weather.gov/xml/current_obs/KSJC.xml).Content).current_observation.temp_f | Out-File c:\temperature.txt
i suspect i am misunderstanding something [this seems alarmingly simple], but here are a few ways to do what it seems you want ... [grin]
$Temperature = '12.3'
$Temperature.Split('.')[0]
[int]$Temperature
[math]::Round([decimal]$Temperature, 0)
the output if each is 12.
the 1st uses the string .Split() method and takes the 1st item in the resulting array
the 2nd uses the [int] type accelerator to coerce the string into an int & rounds it in the process
the 3rd uses the [decimal] type accelerator to coerce the string to a decimal number and the [math]::Round() static method to round the number to 0 decimal places
Related
I am trying to write a Powershell code to identify a string with a specific character from a filename from multiple files.
An example of a filename
20190902091031_202401192_50760_54206_6401.pdf
$Variable = $Filename.Substring(15,9)
Results:
202401192 (this is what I am after)
However in some instances the filename will be like below
20190902091031_20240119_50760_54206_6401.pdf
$Variable = $Filename.Substring(15,9)
Results:
20240119_ (this is NOT what I am after)
I am trying to find a code to identify the 9th character,
IF the 9th character = "_"
THEN Set
$Variable = $Filename.Substring(15,8)
Results:
20240119
All credit to TheMadTechnician who beat me to the punch with this answer.
To expand on the technique a bit, use the split method or operator to split a string every time a certain character shows up. Your data is separated by the underscore character, so is a perfect example of using this technique. By using either of the following:
$FileName.Split('_')
$FileName -split '_'
You can turn your long string into an array of shorter strings, each containing one of the parts of your original string. Since you want the 2nd one, you use the array descriptor [1] (0 is 1st) and you're done.
Good luck
i am facing issue while converting unicode data into national characters.
When i convert the Unicode data into national using national-of function, some junk character like # is appended after the string.
E.g
Ws-unicode pic X(200)
Ws-national pic N(600)
--let the value in Ws-Unicode is これらの変更は. getting from java end.
move function national-of ( Ws-unicode ,1208 ) to Ws-national.
--after converting value is like これらの変更は #.
i do not want the extra # character added after conversion.
please help me to find out the possible solution, i have tried to replace N'#' with space using inspect clause.
it worked well but failed in some specific scenario like if we have # in input from user end. in that case genuine # also converted to space.
Below is a snippet of code I used to convert EBCDIC to UTF. Before I was capturing string lengths, I was also getting # symbols:
STRING
FUNCTION DISPLAY-OF (
FUNCTION NATIONAL-OF (
WS-EBCDIC-STRING(1:WS-XML-EBCDIC-LENGTH)
WS-EBCDIC-CCSID
)
WS-UTF8-CCSID
)
DELIMITED BY SIZE
INTO WS-UTF8-STRING
WITH POINTER WS-XML-UTF8-LENGTH
END-STRING
SUBTRACT 1 FROM WS-XML-UTF8-LENGTH
What this code does is string the UTF8 representation of the EBCIDIC string into another variable. The WITH POINTER clause will capture the new length of the string + 1 (+ 1 because the pointer is positioned to the next position after the string ended).
Using this method, you should be able to know exactly how long second string is and use that string with the exact length.
That should remove the unwanted #s.
EDIT:
One thing I forgot to mention, in my case, the # signs were actually EBCDIC low values when viewing the actual hex on the mainframe
Use inspect with reverse and stop after first occurence of #
I'm very new to powershell and am running into walls trying to convert a string to a integer.
If I run the following command: Get-DefaultAudioDeviceVolume it often returns a number that looks something like: 50.05816%, which I have confirmed to be a string. I need to convert this to a whole number integer (50). Obviously I could hard code the integer in my script, but for the purpose of the script I need it to be flexible in it's conversion. The result of the previous test changes and I want to pass along the whole integer further down the line.
Thanks in advance!
If the string contains the % symbol you would need to remove this, then you can use the -as operator to convert to [int]
[string]$vol = "50.05816%"
$vol_int = $vol.Replace('%','') -as [int]
The -as operator is very useful and has many other uses, this article goes through a number of them: https://mcpmag.com/articles/2013/08/13/utilizing-the-as-operator.aspx
Just cast it to integer and replace the "%" with nothing:
[int]$var = (Get-DefaultAudioDeviceVolume).Replace("%","")
Powershell does automatic type casting and starts from the left. So when $var is defined as an integer, it will try to convert the right side to the same type.
I am trying to convert a string to a 2 decimal place value
I have a script that has a line of code which is
(get-mailboxdatabase xxx -status).databasesize
This returns the size something like
1.008 GB (1,082,195,968 bytes)
I want to be able to convert this to a number (1.008) and can't work out how to do it.
I know about ToGB() but that only works when running the script from EMS.
I need to be able to run the script in Powershell and not EMS as the script does other things.
How do I convert the value to a number?
TIA
Andy
If you're using implicit remoting (it sounds like you are), that's going to be a [string], so you'll need to use string methods.
'1.008 GB (1,082,195,968 bytes)' -replace '^([0-9.]+).+','$1'
1.008
This is very easy, just make a cast and divide with 1GB.
PS C:\> [int]"1082195968"/1GB
1,00787353515625
Or in your case
([int](get-mailboxdatabase xxx -status).databasesize)/1GB
You can convert numbers to the various number types using standard .NET framework types and conversions. For example:
> $var = [decimal]::Parse("1.008")
> $var.GetType().Name
Decimal
Extracting just that portion of the string from the input is a separate issue, and it's hard to tell from your question which is giving you trouble. To get just the number, you might use something like this:
$input = (get-mailboxdatabase xxx -status).databasesize
$strSize = $input.Split(' ')[0]
Or you can use a regular expression. It all depends on how variable your size is.
try this:
$val= (get-mailboxdatabase xxx -status).databasesize
[decimal]([regex]::Match( $val, '(^.+)GB' )).groups[1].value
I want to convert int to string and then concatenate dot with it. Here is the formula
totext({#SrNo})+ "."
It works perfectly but not what i want. I want to show at as
1.
but it shows me in this way
1.00.
it means that when i try to convert int to string it convert it into number with precision of two decimal zeros. Can someone tell me how can i show it in proper format. For information i want to tell you that SrNo is running total.
ToText(x, y, z, w) Function can use
x=The number to convert to text
y=The number of decimal places to include in result (optional). The value will be rounded to that decimal place.
z=The character to use as the thousands separator. If you don’t specify one, it will use your application default. (Optional.)
w=The character to use as the decimal separator. If you don’t specify one, it will use your application default. (Optional.)
Examples
ToText(12345.678) = > “12345.678″
ToText(12345.678,2) = > “12345.67″
ToText(12345.678,0) = > “12345″
You can try this :
totext({fieldname},0)
Ohhh I got the answer it was so simple.
totext takes 4 parameters
First parameter is value which is going to be converted
Second parameter is number of decimal previsions.
Third parameter is decimal separator. like (1,432.123) here dot(.) is third parameter.
Forth parameter is thousand separator. like (1,432) here comma(,) is forth parameter.
Example{
totext("1,432.1234",2) results 1,432.12
totext("1,432.1234",2,' " ') results 1,432"1234
totext("1,432.1234",2,' " ', ' : ') results 1:432,1234
}
Although i think this example may be not so good but i just want to give you an idea. This is for int conversion for date it has 2 parameters.
value to be converted and format of date.