topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday July 15, 2024, 7:18 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Celsius <-> Fahrenheit in PowerShell  (Read 4690 times)

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,481
    • View Profile
    • Donate to Member
Celsius <-> Fahrenheit in PowerShell
« on: November 22, 2022, 05:15 PM »
I had a request to myself: I want to convert Fahrenheit <-> Celsius on Windows without having to install a whole bunch of GNU utilities (which would have been the easiest way, I presume). So I wrote a converter for Powershell which I use anyway. :)

Code: PowerShell [Select]
  1. function FtoC([double]$fahrenheit) {
  2.         $celsius = ($fahrenheit - 32) * (5/9)
  3.         "{0} °F = {1} °C" -f $fahrenheit,[math]::Round($celsius,3)
  4. }
  5.  
  6. function CtoF([double]$celsius) {
  7.         $fahrenheit = ($celsius * (9/5)) + 32
  8.         "{0} °C = {1} °F" -f $celsius,[math]::Round($fahrenheit,3)
  9. }

Usage:

> FtoC(32)
32 °F = 0 °C
> CtoF(0)
0 °C = 32 °F