Temperature conversion: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 35:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V k = 21.0
print(‘K ’k)
print(‘C ’(k - 273.15))
print(‘F ’(k * 1.8 - 459.67))
print(‘R ’(k * 1.8))</langsyntaxhighlight>
 
{{out}}
Line 53:
Use of packed decimal arithmetic
(ZAP,SP,MP,DP,UNPK,CVD,EDMK opcodes).
<syntaxhighlight lang="text">* Temperature conversion 10/09/2015
TEMPERAT CSECT
USING TEMPERAT,R15
Line 125:
EDMASKN DC X'402021204B202060' CL8 5num
YREGS
END TEMPERAT</langsyntaxhighlight>
{{out}}
<pre>
Line 146:
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">: KtoC \ n -- n
273.15 n:-
;
Line 178:
bye
;
</syntaxhighlight>
</lang>
{{out}}
<pre>>8th temp.8th 21
Line 189:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC K2C(REAL POINTER k,c)
Line 241:
ValR("373.15",k) Test("Water boils",k)
RETURN
</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Temperature_conversion.png Screenshot from Atari 8-bit computer]
Line 266:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Float_Text_IO, Ada.Text_IO; use Ada.Float_Text_IO, Ada.Text_IO;
 
procedure Temperatur_Conversion is
Line 279:
Put("F: "); Put(F, Fore => 4, Aft => 2, Exp => 0); New_Line;-- F: dddd.dd
Put("R: "); Put(R, Fore => 4, Aft => 2, Exp => 0); New_Line;-- R: dddd.dd
end;</langsyntaxhighlight>
 
{{out}}
Line 290:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">void
show(integer symbol, real temperature)
{
Line 309:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>aime$ aime -a tmp/tconvert 300
Line 318:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">
BEGIN
REAL kelvin;
Line 326:
printf ((f, kelvin, 9.0 * kelvin / 5.0, "R"));
printf ((f, kelvin, 9.0 * kelvin / 5.0 - 459.67, "F"))
END</langsyntaxhighlight>
{{out}}
<pre>$ echo 21 | a68g Temperature_conversion.a68
Line 336:
=={{header|ALGOL-M}}==
If the temperature in Kelvin is a whole number, you should type a decimal point after it (e.g. <code>290.</code>): <code>290</code> with no decimal point will be interpreted as 0.29 rather than 290.0.
<langsyntaxhighlight lang="algol">BEGIN
DECIMAL K, C, F, R;
WRITE( "Temperature in Kelvin:" );
Line 347:
WRITE( F, " degrees Fahrenheit" );
WRITE( R, " degrees Rankine" );
END</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
/* MISTRAL - a flavour of Hopper */
 
Line 378:
FUNCIÓN( Conversión Kelvin a Rankine, k)
RETORNAR ( {k} POR (1.8), REDONDEADO AL DECIMAL(2) )
</syntaxhighlight>
</lang>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
/* MISTRAL - a flavour of Hopper */
 
Line 393:
IMPRIMIR("RANKINE : ",temperatura, POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
</lang>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <mistral.h>
 
Line 407:
"RANKINE : ", {temperatura} POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
</lang>
<p>Or another (and last) version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <mistral.h>
 
Line 421:
"RANKINE : ", rankine, NL)
FINALIZAR
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 440:
=={{header|APL}}==
Given a temperature in Kelvin, prints the equivalent in Kelvin, Celsius, Fahrenheit, and Rankine (in that order).
<langsyntaxhighlight lang="apl"> CONVERT←{⍵,(⍵-273.15),(R-459.67),(R←⍵×9÷5)}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="apl"> CONVERT 21
21 ¯252.15 ¯421.87 37.8</langsyntaxhighlight>
The "high minus" character <tt>¯</tt> is used in APL to mark negative numbers, preventing any possible confusion with <tt>-</tt> (the subtraction operator).
 
=={{header|AppleScript}}==
{{Trans|JavaScript}} ( ES6 version )
<langsyntaxhighlight AppleScriptlang="applescript">use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function
 
-- KELVIN TO OTHER SCALE -----------------------------------------------------
Line 560:
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower</langsyntaxhighlight>
{{Out}}
<pre>K 21.0
Line 571:
Or of course:
 
<langsyntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
return ("K" & tab & (kelvinValue as real)) & ¬
(linefeed & "C" & tab & (kelvinValue - 273.15)) & ¬
Line 578:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
Vanilla AppleScript actually has a handful of built-in measurement unit coercions, including three for converting between temperatures in Kelvin, Celsius, and Fahrenheit. There isn't one for Rankine, but it's an easy calculation from Kelvin:
 
<langsyntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
set kelvinMeasurement to kelvinValue as degrees Kelvin
Line 595:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
As from macOS 10.12 Sierra, macOS's Foundation framework too offers "Units and Measurement" classes and methods, which can be accessed from AppleScript using ASObjC code. They offer many more categories and units, although it's usually easier, faster, and more efficient (and occasionally more accurate!) to look up the conversion formulae on Wikipedia and write the math directly into the scripts, as above.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.5" -- macOS 10.12 (Sierra) or later
use framework "Foundation"
 
Line 632:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">convertKelvins: function [k][
#[
celcius: k - 273.15
Line 644:
]
 
print convertKelvins 100</langsyntaxhighlight>
 
{{out}}
Line 651:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % "Kelvin:`t`t 21.00 K`n"
. "Celsius:`t`t" kelvinToCelsius(21) " C`n"
. "Fahrenheit:`t" kelvinToFahrenheit(21) " F`n"
Line 667:
{
return, round(k * 1.8, 2)
}</langsyntaxhighlight>
{{out}}
<pre>Kelvin: 21.00 K
Line 675:
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">; ### USAGE - TESTING PURPOSES ONLY
 
Local Const $_KELVIN = 21
Line 694:
Return Round($degrees * 1.8, 2)
EndSelect
EndFunc ;==> Kelvin</langsyntaxhighlight>
{{out}}
<pre>Kelvin: 21°
Line 703:
=={{header|AWK}}==
"Interactive" version, reading from stdin only:
<langsyntaxhighlight AWKlang="awk"># syntax: AWK -f TEMPERATURE_CONVERSION.AWK
BEGIN {
while (1) {
Line 721:
}
exit(0)
}</langsyntaxhighlight>
 
"Regular" version, reading from input-file(s). <br>
Line 727:
 
{{works with|gawk}} BEGINFILE is a gawk-extension
<langsyntaxhighlight AWKlang="awk"># usage: gawk -f temperature_conversion.awk input.txt -
 
BEGIN { print("# Temperature conversion\n") }
Line 746:
 
END { print("# Bye.") }
</syntaxhighlight>
</lang>
 
{{out|Input}} the numeric value of the first word in each line is used as input for the conversion
Line 820:
=={{header|BASIC}}==
 
<langsyntaxhighlight lang="basic">
10 REM TRANSLATION OF AWK VERSION
20 INPUT "KELVIN DEGREES",K
Line 832:
100 PRINT R; " DEGREES RANKINE"
110 GOTO 20
</syntaxhighlight>
</lang>
 
==={{header|QBasic}}===
<syntaxhighlight lang="qbasic">DO
<lang QBasic>DO
INPUT "Kelvin degrees (>=0): ", K
LOOP UNTIL K >= 0
Line 843:
PRINT "F = " + STR$(K * 1.8 - 459.67)
PRINT "R = " + STR$(K * 1.8)
END</langsyntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang="qbasic">DO
PRINT "Kelvin degrees (>=0): ";
INPUT K
Line 856:
PRINT "F = "; STR$(K * 1.8 - 459.67)
PRINT "R = "; STR$(K * 1.8)
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">repeat
input "Kelvin degrees (>=0): " K
until K >= 0
Line 867:
print "F = " + str$(K * 1.8 - 459.67)
print "R = " + str$(K * 1.8)
end</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 INPUT PROMPT "Kelvin degrees: ":K
110 PRINT K;TAB(10);"Kelvin is equivalent to"
120 PRINT K-273.15;TAB(10);"Degrees Celsius"
130 PRINT K*1.8-459.67;TAB(10);"Degrees Fahrenheit"
140 PRINT K*1.8;TAB(10);"Degrees Rankine"</langsyntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
<langsyntaxhighlight lang="basic">10 PRINT "ENTER A TEMPERATURE IN KELVINS"
20 INPUT K
30 PRINT K;" KELVINS ="
40 PRINT K-273.15;" DEGREES CELSIUS"
50 PRINT K*1.8-459.67;" DEGREES FAHRENHEIT"
60 PRINT K*1.8;" DEGREES RANKINE"</langsyntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<langsyntaxhighlight lang="tiny basic">
PRINT "Temperature in Kelvin?"
INPUT K
Line 893:
PRINT C," Celsius"
PRINT F," Fahrenheit"
PRINT R," Rankine"</langsyntaxhighlight>
 
=={{header|BASIC256}}==
<langsyntaxhighlight lang="basic256">
do
print "Kelvin degrees (>=0): ";
Line 906:
print "F = " + string(K * 1.8 - 459.67)
print "R = " + string(K * 1.8)
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic">
REPEAT
INPUT "Kelvin degrees (>=0): " K
Line 919:
PRINT "R = " K * 1.8
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 933:
The temperature to convert is read from stdin. Befunge has no support for real numbers, though, so reading and writing of decimal values is done with character I/O. For the same reason, the temperature calculations use integer arithmetic to emulate fixed point. The first two lines handle the input; the second line performs the conversion calculations; and the last three handle the output.
 
<langsyntaxhighlight lang="befunge">0000>0p~>"."-:!#v_2-::0\`\9`+!#v_$1>/\:3`#v_\>\:3 \`#v_v
1#<<^0 /2++g001!<1 \+g00\+*+55\< ^+55\-1< ^*+55\+1<v_
"K"\-+**"!Y]"9:\"C"\--\**"^CIT"/5*9:\"F"\/5*9:\"R"\0\0<v
v/+55\+*86%+55: /+55\+*86%+55: \0/+55+5*-\1*2 p00:`\0:,<
>"."\>:55+% 68*v >:#,_$55+,\:!#@_^
$_^#!:/+55\+< ^\" :"_<g00*95 </langsyntaxhighlight>
 
{{out}}
Line 948:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( rational2fixedpoint
= minus fixedpointnumber number decimals
. !arg:(#?number.~<0:~/#?decimals)
Line 993:
)
& done!
)</langsyntaxhighlight>
{{out}}
<pre>Enter Kelvin temperature:21.00
Line 1,003:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 1,031:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace TemperatureConversion
Line 1,064:
}
}
}</langsyntaxhighlight>
 
<pre>
Line 1,075:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <iomanip>
Line 1,130:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,149:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
 
void printKelvinConversions(Float kelvin) {
Line 1,164:
printKelvinConversions(21.0);
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{trans|Common Lisp}}
<langsyntaxhighlight lang="clojure">(defn to-celsius [k]
(- k 273.15))
(defn to-fahrenheit [k]
Line 1,179:
(format "Celsius: %.2f Fahrenheit: %.2f Rankine: %.2f"
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format "Error: Non-numeric value entered.")))</langsyntaxhighlight>
 
{{out}}
Line 1,188:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">kelvin = proc (k: real) returns (real)
return(k)
end kelvin
Line 1,225:
stream$putl(po, f_form(c.func(k), 6, 2))
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>Enter temperature in Kelvin: 21
Line 1,235:
=={{header|COBOL}}==
{{works with|Visual COBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. temp-conversion.
Line 1,270:
GOBACK
.</langsyntaxhighlight>
 
{{out}}
Line 1,284:
Three functions define the necessary conversion formulas. A fancy format string is used to print these values.
 
<langsyntaxhighlight lang="lisp">
(defun to-celsius (k)
(- k 273.15))
Line 1,298:
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format t "Error: Non-numeric value entered."))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,311:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">double kelvinToCelsius(in double k) pure nothrow @safe {
return k - 273.15;
}
Line 1,343:
writefln("%2.2f K is below absolute zero", kelvin);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,356:
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">
program Temperature;
 
Line 1,398:
readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,409:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">k = number input
print k & " °K"
print k - 273.15 & " °C"
print k * 1.8 - 459.67 & " °F"
print k * 1.8 & " °R"</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.1 :
<langsyntaxhighlight lang="elena">import extensions;
convertKelvinToFahrenheit(x)
Line 1,449:
console.printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp));
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,460:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Temperature do
def conversion(t) do
IO.puts "K : #{f(t)}"
Line 1,475:
end
 
Temperature.task</langsyntaxhighlight>
 
{{out}}
Line 1,489:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(temp_conv).
-export([main/0]).
Line 1,504:
f(A) ->
(round(A*100))/100 .
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,519:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="openeuphoria">
<lang OpenEuphoria>
include std/console.e
 
Line 1,527:
printf(1,"K = %5.2f\nC = %5.2f\nF = %5.2f\nR = %5.2f\n\n",{K,K-273.15,K*1.8-459.67,K*1.8})
end while
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,540:
 
=={{header|Excel}}==
<syntaxhighlight lang="text">A1 : Kelvin
B1 : Celsius
C1 : Fahrenheit
Line 1,548:
C2 : =K*1.8-459.67
D2 : =K*1.8
Input in A1 </langsyntaxhighlight>
{{Out}}
<pre>
Line 1,565:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">FROMKELVIN
=LAMBDA(toUnit,
LAMBDA(n,
Line 1,580:
)
)
)</langsyntaxhighlight>
 
The example below generates the spaced list of test values on the left from the expression ENUMFROMTHENTO(240)(250)(390),
applying the following custom function:
<langsyntaxhighlight lang="lisp">ENUMFROMTHENTO
=LAMBDA(a,
LAMBDA(b,
Line 1,598:
)
)
)</langsyntaxhighlight>
 
The four columns on the right of the output read their target format from the label cell at the top of each column.
Line 1,737:
 
=={{header|Ezhil}}==
<syntaxhighlight lang="ezhil">
<lang Ezhil>
# convert from Kelvin
நிரல்பாகம் கெல்வின்_இருந்து_மாற்று( k )
Line 1,746:
கெல்வின்_இருந்து_மாற்று( 273 ) #freezing pt of water
கெல்வின்_இருந்து_மாற்று( 30 + 273 ) #room temperature in Summer
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Define units of measure
[<Measure>] type k
Line 1,766:
printfn "%A Kelvin is %A Fahrenheit" K (kelvinToFahrenheit K)
printfn "%A Kelvin is %A Rankine" K (kelvinToRankine K)
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: combinators formatting kernel math ;
IN: rosetta-code.temperature
 
Line 1,780:
"K %.2f\nC %.2f\nF %.2f\nR %.2f\n" printf ;
21 convert</langsyntaxhighlight>
{{out}}
<pre>
Line 1,790:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 ASK "TEMPERATURE IN KELVIN", K
01.20 TYPE "K ", %6.02, K, !
01.30 TYPE "C ", %6.02, K - 273.15, !
01.40 TYPE "F ", %6.02, K * 1.8 - 459.67, !
01.50 TYPE "R ", %6.02, K * 1.8, !</langsyntaxhighlight>
{{out}}
<pre>TEMPERATURE IN KELVIN:373.15
Line 1,804:
=={{header|Forth}}==
{{works with|GNU Forth}} for the command line handling
<langsyntaxhighlight lang="forth">: k>°C ( F: kelvin -- celsius ) 273.15e0 f- ;
: k>°R ( F: kelvin -- rankine ) 1.8e0 f* ;
: °R>°F ( F: rankine -- fahrenheit ) 459.67e0 f- ;
Line 1,816:
then ;
 
main bye</langsyntaxhighlight>
{{out}}
<pre>&gt; gforthamd64 rosetta_temp_conv.fs 21
Line 1,826:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">Program Temperature
implicit none
Line 1,851:
 
end subroutine
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub convKelvin(temp As Double)
Line 1,873:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,889:
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim fKelvin As Float
 
Line 1,899:
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,909:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,935:
fmt.Printf("F %.2f\n", k*9/5-459.67)
fmt.Printf("R %.2f\n", k*9/5)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,946:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
class Convert{
static void main(String[] args){
Line 1,959:
static def k_to_r(def k=21.0){return k*1.8;}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,970:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import System.Exit (die)
import Control.Monad (mapM_)
 
Line 1,985:
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (*1.8)]
nums = (show . ($n)) <$> conversions</langsyntaxhighlight>
 
Or with properly managed exceptions:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE LambdaCase #-}
 
import System.Exit (die)
Line 2,012:
t <- liftIO getLine >>= tryRead "Could not read temp"
tryAssert "Temp cannot be negative" (t>=0)
return t</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
The following program works in both languages:
<langsyntaxhighlight lang="unicon">procedure main(A)
k := A[1] | 21.00
write("K ",k)
Line 2,023:
write("R ",r := k*(9.0/5.0))
write("F ",r - 459.67)
end</langsyntaxhighlight>
 
Sample runs:
Line 2,042:
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j"> NB. Temp conversions are all linear polynomials
K2K =: 0 1 NB. K = (1 *k) + 0
K2C =: _273 1 NB. C = (1 *k) - 273
Line 2,052:
NB. numeric matrix J programs would manipulate
NB. directly.
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="j"> NB. Format matrix for printing & tag each
NB. temp with scale, for human legibility
fmt =: [: (;:inv"1) 0 _1 |: 'KCFR' ;"0 1"_1 '0.2' 8!:0 ]
Line 2,075:
C -252.00 -173.00 27.00
F -421.87 -279.67 80.33
R 37.80 180.00 540.00</langsyntaxhighlight>
'''Notes''': The approach is founded on polynomials, one for each conversion (e.g. <tt>Fahrenheit = 1.8*x - 459.67</tt> where <tt>x</tt> is measured in degrees Kelvin), and all polynomials are evaluated simultaneously using the built-in <code>p.</code>. Through some code decorations (specifically the <code>/</code> in <code>p./</code> the <code>"0 1"_1</code> and the <code>0 _1 |:</code>), we permit our function to convert arrays of temperatures of arbitrarily high dimension (a single temp, lists of temps, tables of temps, cubes of temps, etc).
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class TemperatureConversion {
public static void main(String args[]) {
if (args.length == 1) {
Line 2,109:
return k * 1.8;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,124:
 
===ES5===
<langsyntaxhighlight lang="javascript">var k2c = k => k - 273.15
var k2r = k => k * 1.8
var k2f = k => k2r(k) - 459.67
Line 2,137:
kCnv(21)
kCnv(295)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,148:
Deriving '''kelvinTranslations()''' from a more general '''heatBabel()''' function.
 
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,186:
 
})();
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,196:
 
=={{header|jq}}==
The hard part here is defining round/1 generically.<langsyntaxhighlight lang="jq">
# round(keep) takes as input any jq (i.e. JSON) number and emits a string.
# "keep" is the desired maximum number of numerals after the decimal point,
Line 2,233:
end;
 
cfr</langsyntaxhighlight>
'''Example'''
<langsyntaxhighlight lang="sh"> $ jq -M -r -f Temperature_conversion.jq
21
Kelvin: 21
Line 2,243:
-1
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">cfr(k) = print("Kelvin: $k, ",
"Celsius: $(round(k-273.15,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Rankine: $(round(k*1.8,2))")</langsyntaxhighlight>
<pre>julia> cfr(21)
Kelvin: 21, Celsius: -252.15, Fahrenheit: -421.87, Rankine: 37.8</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Kelvin(val degrees: Double) {
Line 2,274:
println("F ${f.format(k.toFahreneit())}\n")
println("R ${f.format(k.toRankine())}")
}</langsyntaxhighlight>
 
{{out}}
Line 2,290:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def to-celsius {lambda {:k} {- :k 273.15}}}
-> to-celsius
Line 2,317:
-421.87 farenheit
37.8 rankine
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define tempconverter(temp, kind) => {
 
local(
Line 2,370:
tempconverter(37.80, 'r')
'<br />'
tempconverter(69.80, 'f')</langsyntaxhighlight>
<pre>K = 21.00 C = -252.15 R = 37.80 F = -421.87
K = 294.15 C = 21.00 R = 529.47 F = 69.80
Line 2,378:
 
=={{header|LIL}}==
<langsyntaxhighlight lang="tcl"># Temperature conversion, in LIL
func kToc k {expr $k - 273.15}
func kTor k {expr $k / 5.0 * 9.0}
Line 2,389:
print "Fahrenheit: [kTof $k]"
print "Rankine: [kTor $k]"
}</langsyntaxhighlight>
 
{{out}}
Line 2,400:
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function convertDegrees k
put k/5 * 9 into r
put k - 273.15 into c
put r - 459.67 into f
return k,r,c,f
end convertDegrees</langsyntaxhighlight>
Example<langsyntaxhighlight LiveCodelang="livecode">put convertDegrees(21.00) into tTemp
put item 1 of tTemp into temperature["Kelvin"]
put item 2 of tTemp into temperature["Rankine"]
Line 2,414:
put temperature
 
-- Celsius:-252.15,Fahrenheit:-421.87,Kelvin:21.00,Rankine:37.8</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang="lua">function convert_temp(k)
local c = k - 273.15
local r = k * 1.8
Line 2,430:
Rankine: %.2f °R
Fahrenheit: %.2f °F
]],convert_temp(21.0)))</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
 
<syntaxhighlight lang="liberty basic">Do
<lang Liberty BASIC>Do
Input "Kelvin degrees (>=0): ";K
Loop Until (K >= 0)
Line 2,442:
Print "F = ";(K * 1.8 - 459.67)
Print "R = ";(K * 1.8)
End</langsyntaxhighlight>
 
{{out}}
Line 2,453:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">tempConvert := proc(k)
seq(printf("%c: %.2f\n", StringTools[UpperCase](substring(i, 1)), convert(k, temperature, kelvin, i)), i in [kelvin, Celsius, Fahrenheit, Rankine]);
return NULL;
end proc:
 
tempConvert(21);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,468:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">tempConvert[t_] := # -> Thread@UnitConvert[#,{"DegreesFahrenheit", "DegreesCelsius", "DegreesRankine"}]&@Quantity[N@t, "Kelvins"]
tempConvert[21]</langsyntaxhighlight>
{{out}}
<pre>21.K -> {-421.87°F,-252.15°C,37.8°R}</pre>
Line 2,475:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(
((float) (273.15 -) (9 5 / * 459.67 -) (9 5 / *)) cleave
() 'cons 4 times "K $1\nC $2\nF $3\nR $4" swap % puts!
) :convert
 
21 convert</langsyntaxhighlight>
{{out}}
<pre>
Line 2,490:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">fromKelvin = function(temp)
print temp + " degrees in Kelvin is:"
Celsius = temp - 273.15
Line 2,501:
 
temp = input("Enter a temperature in Kelvin: ")
fromKelvin temp.val</langsyntaxhighlight>
{{out}}
<pre>
Line 2,518:
 
=={{header|MiniZinc}}==
<langsyntaxhighlight MiniZinclang="minizinc">float: kelvin;
 
var float: celsius;
Line 2,529:
solve satisfy;
 
output ["K \(kelvin)\n", "C \(celsius)\n", "F \(fahrenheit)\n", "R \(rankine)\n"];</langsyntaxhighlight>
{{out}}<pre>
Compiling temperature.mzn, additional arguments kelvin=1000;
Line 2,542:
 
=={{header|МК-61/52}}==
<langsyntaxhighlight lang="mk61">П7 0 , 8 * П8 ИП7 9 * 5
/ 3 2 + П9 ИП7 2 7 3 ,
1 5 + П4 С/П П8 1 , 8 /
БП 00 П9 3 2 - 5 * 9 /
БП 00 П4 2 7 3 , 1 5 -
БП 00</langsyntaxhighlight>
 
''Instruction:''
Line 2,572:
==={{header|mLite}}===
Temperature in Kelvin given on command line.
<langsyntaxhighlight lang="ocaml">fun KtoC n = n - 273.15;
fun KtoF n = n * 1.8 - 459.67;
fun KtoR n = n * 1.8;
Line 2,588:
print "Rankine: "; println ` KtoR K
end
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight Nanoquerylang="nanoquery">% while true
... print "K ? "
... k = float(input())
Line 2,602:
K ? 222.2
222.200 Kelvin = -50.9500 Celsius = -59.7100 Fahrenheit = 399.960 Rankine degrees.
K ? </langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols
 
Line 2,790:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre pre style="height: 40ex; overflow: scroll">
Line 2,855:
 
=={{header|Never}}==
<syntaxhighlight lang="never">
<lang Never>
func KtoC(k : float) -> float { k - 273.15 }
func KtoF(k : float) -> float { k * 1.8 - 459.67 }
Line 2,872:
0
}
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,882:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(define (to-celsius k)
(- k 273.15)
Line 2,904:
)
)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,915:
=={{header|Nim}}==
{{libheader|strfmt}}
<langsyntaxhighlight lang="nim">import rdstdin, strutils, strfmt
 
while true:
let k = parseFloat readLineFromStdin "K ? "
echo "{:g} Kelvin = {:g} Celsius = {:g} Fahrenheit = {:g} Rankine degrees".fmt(
k, k - 273.15, k * 1.8 - 459.67, k * 1.8)</langsyntaxhighlight>
Sample usage:
<pre>K ? 21.0
Line 2,928:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class Temperature {
function : Main(args : String[]) ~ Nil {
Line 2,954:
}
}
</syntaxhighlight>
</lang>
 
<pre>
Line 2,964:
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[])
Line 2,982:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">
let print_temp s t =
print_string s;
Line 3,006:
print_temp "F " (kelvin_to_fahrenheit k);
print_temp "R " (kelvin_to_rankine k);;
</syntaxhighlight>
</lang>
 
Sample session:
Line 3,020:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: kelvinToCelsius 273.15 - ;
: kelvinToFahrenheit 1.8 * 459.67 - ;
: kelvinToRankine 1.8 * ;
Line 3,027:
n kelvinToCelsius println
n kelvinToFahrenheit println
n kelvinToRankine println ;</langsyntaxhighlight>
 
{{out}}
Line 3,038:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">f(x)=[x,x-273.15,1.8*x-459.67,1.8*x]</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight Pascallang="pascal">program TemperatureConvert;
 
type
Line 3,094:
writeln(' ', ConvertTemperature(kelvin, K, F) : 3 : 2, ' in degrees Fahrenheit.');
writeln(' ', ConvertTemperature(kelvin, K, R) : 3 : 2, ' in degrees Rankine.');
end.</langsyntaxhighlight>
 
{{out}}
Line 3,106:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">my %scale = (
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Line 3,118:
foreach (sort keys %scale) {
printf "%12s:%8.2f\n", $_, $kelvin * $scale{$_}{factor} + $scale{$_}{offset};
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,129:
=={{header|Phix}}==
Modified copy of [[Temperature_conversion#Euphoria|Euphoria]]
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">atom</span> <span style="color: #000000;">K</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prompt_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Enter temperature in Kelvin >=0: "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e307</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Kelvin: %5.2f\n Celsius: %5.2f\nFahrenheit: %5.2f\n Rankine: %5.2f\n\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">K</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">-</span><span style="color: #000000;">273.15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">-</span><span style="color: #000000;">459.67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,144:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
 
while (true) {
Line 3,163:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Enter a value in kelvin (q to quit): 21
Line 3,175:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(de convertKelvin (Kelvin)
Line 3,186:
(tab (-3 8)
(car X)
(format ((cdr X) Kelvin) *Scl) ) ) )</langsyntaxhighlight>
Test:
<syntaxhighlight lang PicoLisp="picolisp">(convertKelvin 21.0)</langsyntaxhighlight>
{{out}}
<pre>K 21.00
Line 3,196:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref;
/* PL/I **************************************************************
* 15.08.2013 Walter Pachl translated from NetRexx
Line 3,343:
End;
End;
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 3,359:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Put 21 into a kelvin temperature.
Line 3,392:
Show the celsius temperature given "C".
Show the fahrenheit temperature given "F".
Show the rankine temperature given "R".</langsyntaxhighlight>
{{out}}
<pre>
Line 3,403:
=={{header|PowerShell}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="powershell">function temp($k){
try{
$c = $k - 273.15
Line 3,423:
 
$input=Read-host "Enter a temperature in Kelvin"
temp $input</langsyntaxhighlight>
{{Out}}
<pre>PS> ./TEMPS
Line 3,437:
===PowerShell Alternate Version===
A more "PowerShelly" way to do it.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Convert-Kelvin
{
Line 3,465:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
21, 100 | Convert-Kelvin
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,571:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">Procedure.d Kelvin2Celsius(tK.d) : ProcedureReturn tK-273.15 : EndProcedure
Procedure.d Kelvin2Fahrenheit(tK.d) : ProcedureReturn tK*1.8-459.67 : EndProcedure
Procedure.d Kelvin2Rankine(tK.d) : ProcedureReturn tK*1.8 : EndProcedure
Line 3,586:
k$=Inkey() : Delay(50) : If RawKey()=#ESC : End : EndIf
Until RawKey()
ForEver</langsyntaxhighlight>
<pre>Temperatur Kelvin? 21
Conversion:
Line 3,595:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> while True:
k = float(input('K ? '))
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
Line 3,605:
K ? 222.2
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
K ? </langsyntaxhighlight>
 
===Python: Universal conversion===
This converts from any one of the units to all the others
<langsyntaxhighlight lang="python">>>> toK = {'C': (lambda c: c + 273.15),
'F': (lambda f: (f + 459.67) / 1.8),
'R': (lambda r: r / 1.8),
Line 3,628:
<value> <K/R/F/C> ? 399.96 R
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? </langsyntaxhighlight>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
<lang QB64>
Dim As Single Celsius, Kelvin, Fahrenheit, Rankine
 
Line 3,649:
Kelvin = -300
Wend
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 3,657:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
[ 5 9 v* ] is r->k ( n/d --> n/d )
Line 3,687:
say " degrees Rankine" cr ] is task ( $ --> )
$ "21.00" task</langsyntaxhighlight>
 
{{out}}
Line 3,698:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">convert_Kelvin <- function(K){
if (!is.numeric(K))
stop("\n Input has to be numeric")
Line 3,712:
convert_Kelvin(21)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,732:
Although not exactly the shortest code,
the converter function can turn any temperature into any other
<langsyntaxhighlight Racketlang="racket">#lang racket
(define (converter temp init final)
(define to-k
Line 3,757:
;Fahrenheit: -421.87
;Rankine: 37.800000000000004
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 3,763:
 
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>my %scale =
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Line 3,774:
for %scale.sort {
printf "%12s: %7.2f\n", .key, $kelvin * .value<factor> + .value<offset>;
}</langsyntaxhighlight>
 
{{out}}
Line 3,786:
Alternative version that accepts the input in any of the four scales:
 
<syntaxhighlight lang="raku" perl6line>while my $answer = prompt 'Temperature: ' {
my $k = do given $answer {
when s/:i C $// { $_ + 273.15 }
Line 3,798:
say " { $k * 1.8 - 459.67 }℉";
say " { $k * 1.8 }R";
}</langsyntaxhighlight>
{{out}}
<pre>Temperature: 0
Line 3,846:
::* comments (annotation notes) allowed within the list
::* aligned output (whole numbers and decimal fractions)
<langsyntaxhighlight lang="rexx">/*REXX program converts temperatures for a number (8) of temperature scales. */
numeric digits 120 /*be able to support some huge numbers.*/
parse arg tList /*get the specified temperature list. */
Line 3,962:
otherwise call serr 'illegal temperature scale:' y
end /*select*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 98.6F to C, &nbsp; -40C, 0 c (water freezes), &nbsp; 37C (body temp), &nbsp; 100 C (water boils), &nbsp; 21 degrees Kelvin, &nbsp; 0 K (outer space?) </tt>}}
<pre>
Line 4,162:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
k = 21.0 c = 0 r = 0 f = 0
convertTemp(k)
Line 4,174:
r = k * 1.8
f = r - 459.67
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">module TempConvert
 
FROM_TEMP_SCALE_TO_K =
Line 4,223:
end
end</langsyntaxhighlight>
Converts all eight scales to any other scale, by means of method_missing.
 
Usage:
<langsyntaxhighlight lang="ruby">TempConvert.kelvin_to_celsius 100 #=> -173.15
TempConvert.kelvin_to_fahrenheit 100 #=> -279.67
TempConvert.kelvin_to_rankine 100 #=> 180.0
Line 4,237:
TempConvert.newton_to_celsius 100 #=> 303.03
TempConvert.newton_to_fahrenheit 100 #=> 577.45
# All 64 combinations possible</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">[loop]
input "Kelvin Degrees";kelvin
if kelvin <= 0 then end ' zero or less ends the program
Line 4,247:
rankine = kelvin * 1.8
print kelvin;" kelvin is equal to ";celcius; " degrees celcius and ";fahrenheit;" degrees fahrenheit and ";rankine; " degrees rankine"
goto [loop]</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">object TemperatureConversion extends App {
 
def kelvinToCelsius(k: Double) = k + 273.15
Line 4,277:
}
} else println("Temperature not given.")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,287:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main() -> std::io::Result<()> {
print!("Enter temperature in Kelvin to convert: ");
let mut input = String::new();
Line 4,307:
 
Ok(())
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme read)
Line 4,331:
(display "Fahrenheit: ") (display (kelvin->fahrenheit k)) (newline)
(display "Rankine : ") (display (kelvin->rankine k)) (newline)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,342:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,364:
writeln("F: " <& fahrenheit(kelvin) digits 2 lpad 7);
writeln("R: " <& rankine(kelvin) digits 2 lpad 7);
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,377:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var scale = Hash(
Celcius => Hash.new(factor => 1 , offset => -273.15 ),
Rankine => Hash.new(factor => 1.8, offset => 0 ),
Line 4,388:
scale.keys.sort.each { |key|
printf("%12s:%8.2f\n", key, kelvin*scale{key}{:factor} + scale{key}{:offset});
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,399:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">
func KtoC(kelvin : Double)->Double{
Line 4,423:
var r=KtoR(kelvin : k)
print("\(r) Rankine")
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc temps {k} {
set c [expr {$k - 273.15}]
set r [expr {$k / 5.0 * 9.0}]
set f [expr {$r - 459.67}]
list $k $c $f $r
}</langsyntaxhighlight>
 
 
Demonstrating:
<langsyntaxhighlight lang="tcl">puts -nonewline "Enter a temperature in K: "
flush stdout
lassign [temps [gets stdin]] k c f r
Line 4,441:
puts [format "C: %.2f" $c]
puts [format "F: %.2f" $f]
puts [format "R: %.2f" $r]</langsyntaxhighlight>
{{out}}
<pre>
Line 4,455:
==={{header|Korn Shell}}===
{{works with|ksh}}
<langsyntaxhighlight lang="bash">#!/bin/ksh
# Temperature conversion
typeset tt[1]=0.00 tt[2]=273.15 tt[3]=373.15
Line 4,466:
echo "Fahrenheit: $((t*18/10-459.67)) F"
echo "Rankine: $((t*18/10)) R"
done</langsyntaxhighlight>
 
==={{header|bash}}===
{{works with|Bourne Again SHell}}
<langsyntaxhighlight lang="bash">#!/bin/bash
# Temperature conversion
tt[1]=0.00; tt[2]=273.15; tt[3]=373.15
Line 4,481:
echo "Fahrenheit: $(bc<<<"scale=2;$t*18/10-459.67") F"
echo "Rankine: $(bc<<<"scale=2;$t*18/10") R"
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl double k
while true
out "Temp. in Kelvin? " console
Line 4,490:
out "K\t" k endl "C\t" (- k 273.15) endl console
out "F\t" (- (* k 1.8) 459.67) endl "R\t" (* k 1.8) endl endl console
end while</langsyntaxhighlight>
 
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 4,519:
End Select
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>Input in Kelvin : 21,00
Line 4,528:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
WScript.StdOut.Write "Enter the temperature in Kelvin:"
tmp = WScript.StdIn.ReadLine
Line 4,548:
rankine = (k-273.15)*1.8+491.67
End Function
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,561:
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">#DEFINE ABSZC 273.16
#DEFINE ABSZF 459.67
LOCAL k As Double, c As Double, f As Double, r As Double, n As Integer, ;
Line 4,585:
ENDDO
SET FIXED &cf
SET DECIMALS TO n</langsyntaxhighlight>
{{out}}
<pre>
Line 4,596:
=={{header|Vlang}}==
Note: round_sig in 0.3 or later
<langsyntaxhighlight lang="vlang">import math
 
fn main() {
Line 4,608:
rankine := math.round_sig(kelvin * 1.8, 2)
return celsius.str(), fahrenheit.str(), rankine.str()
}</langsyntaxhighlight>
 
{{out}}
Line 4,619:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var tempConv = Fn.new { |k|
Line 4,633:
 
var ks = [0, 21, 100]
for (k in ks) tempConv.call(k)</langsyntaxhighlight>
 
{{out}}
Line 4,654:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="xlisp">(DEFUN CONVERT-TEMPERATURE ()
(SETQ *FLONUM-FORMAT* "%.2f")
(DISPLAY "Enter a temperature in Kelvin.")
Line 4,666:
(DISPLAY `(F = ,(- (* K 1.8) 459.67)))
(NEWLINE)
(DISPLAY `(R = ,(* K 1.8))))</langsyntaxhighlight>
{{out}}
<pre>(CONVERT-TEMPERATURE)
Line 4,677:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
real K, C, F, R;
[ChOut(0, ^K); K:= RlIn(0);
Line 4,686:
R:= F + 459.67;
ChOut(0, ^R); RlOut(0, R); CrLf(0);
]</langsyntaxhighlight>
{{out}}
<pre>
Line 4,696:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">K:=ask(0,"Kelvin: ").toFloat();
println("K %.2f".fmt(K));
println("F %.2f".fmt(K*1.8 - 459.67));
println("C %.2f".fmt(K - 273.15));
println("R %.2f".fmt(K*1.8));</langsyntaxhighlight>
{{out}}
<pre>
Line 4,712:
=={{header|ZX Spectrum Basic}}==
 
<langsyntaxhighlight lang="zxbasic">10 REM Translation of traditional basic version
20 INPUT "Kelvin Degrees? ";k
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
Line 4,722:
90 PRINT f; " Degrees Fahrenheit"
100 PRINT r; " Degrees Rankine"
110 GO TO 20</langsyntaxhighlight>
10,333

edits