Temperature conversion: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 35: Line 35:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>V k = 21.0
<syntaxhighlight lang="11l">V k = 21.0
print(‘K ’k)
print(‘K ’k)
print(‘C ’(k - 273.15))
print(‘C ’(k - 273.15))
print(‘F ’(k * 1.8 - 459.67))
print(‘F ’(k * 1.8 - 459.67))
print(‘R ’(k * 1.8))</lang>
print(‘R ’(k * 1.8))</syntaxhighlight>


{{out}}
{{out}}
Line 53: Line 53:
Use of packed decimal arithmetic
Use of packed decimal arithmetic
(ZAP,SP,MP,DP,UNPK,CVD,EDMK opcodes).
(ZAP,SP,MP,DP,UNPK,CVD,EDMK opcodes).
<lang>* Temperature conversion 10/09/2015
<syntaxhighlight lang="text">* Temperature conversion 10/09/2015
TEMPERAT CSECT
TEMPERAT CSECT
USING TEMPERAT,R15
USING TEMPERAT,R15
Line 125: Line 125:
EDMASKN DC X'402021204B202060' CL8 5num
EDMASKN DC X'402021204B202060' CL8 5num
YREGS
YREGS
END TEMPERAT</lang>
END TEMPERAT</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 146: Line 146:


=={{header|8th}}==
=={{header|8th}}==
<lang forth>: KtoC \ n -- n
<syntaxhighlight lang="forth">: KtoC \ n -- n
273.15 n:-
273.15 n:-
;
;
Line 178: Line 178:
bye
bye
;
;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>>8th temp.8th 21
<pre>>8th temp.8th 21
Line 189: Line 189:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit


PROC K2C(REAL POINTER k,c)
PROC K2C(REAL POINTER k,c)
Line 241: Line 241:
ValR("373.15",k) Test("Water boils",k)
ValR("373.15",k) Test("Water boils",k)
RETURN
RETURN
</lang>
</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Temperature_conversion.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Temperature_conversion.png Screenshot from Atari 8-bit computer]
Line 266: Line 266:
=={{header|Ada}}==
=={{header|Ada}}==


<lang Ada>with Ada.Float_Text_IO, Ada.Text_IO; use Ada.Float_Text_IO, Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Float_Text_IO, Ada.Text_IO; use Ada.Float_Text_IO, Ada.Text_IO;


procedure Temperatur_Conversion is
procedure Temperatur_Conversion is
Line 279: Line 279:
Put("F: "); Put(F, Fore => 4, Aft => 2, Exp => 0); New_Line;-- F: dddd.dd
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
Put("R: "); Put(R, Fore => 4, Aft => 2, Exp => 0); New_Line;-- R: dddd.dd
end;</lang>
end;</syntaxhighlight>


{{out}}
{{out}}
Line 290: Line 290:


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>void
<syntaxhighlight lang="aime">void
show(integer symbol, real temperature)
show(integer symbol, real temperature)
{
{
Line 309: Line 309:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>aime$ aime -a tmp/tconvert 300
<pre>aime$ aime -a tmp/tconvert 300
Line 318: Line 318:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>
<syntaxhighlight lang="algol68">
BEGIN
BEGIN
REAL kelvin;
REAL kelvin;
Line 326: Line 326:
printf ((f, kelvin, 9.0 * kelvin / 5.0, "R"));
printf ((f, kelvin, 9.0 * kelvin / 5.0, "R"));
printf ((f, kelvin, 9.0 * kelvin / 5.0 - 459.67, "F"))
printf ((f, kelvin, 9.0 * kelvin / 5.0 - 459.67, "F"))
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>$ echo 21 | a68g Temperature_conversion.a68
<pre>$ echo 21 | a68g Temperature_conversion.a68
Line 336: Line 336:
=={{header|ALGOL-M}}==
=={{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.
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.
<lang algol>BEGIN
<syntaxhighlight lang="algol">BEGIN
DECIMAL K, C, F, R;
DECIMAL K, C, F, R;
WRITE( "Temperature in Kelvin:" );
WRITE( "Temperature in Kelvin:" );
Line 347: Line 347:
WRITE( F, " degrees Fahrenheit" );
WRITE( F, " degrees Fahrenheit" );
WRITE( R, " degrees Rankine" );
WRITE( R, " degrees Rankine" );
END</lang>
END</syntaxhighlight>


=={{header|Amazing Hopper}}==
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
/* MISTRAL - a flavour of Hopper */
/* MISTRAL - a flavour of Hopper */


Line 378: Line 378:
FUNCIÓN( Conversión Kelvin a Rankine, k)
FUNCIÓN( Conversión Kelvin a Rankine, k)
RETORNAR ( {k} POR (1.8), REDONDEADO AL DECIMAL(2) )
RETORNAR ( {k} POR (1.8), REDONDEADO AL DECIMAL(2) )
</syntaxhighlight>
</lang>
<p>Another version:</p>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
/* MISTRAL - a flavour of Hopper */
/* MISTRAL - a flavour of Hopper */


Line 393: Line 393:
IMPRIMIR("RANKINE : ",temperatura, POR '1.8', NL)
IMPRIMIR("RANKINE : ",temperatura, POR '1.8', NL)
FINALIZAR
FINALIZAR
</syntaxhighlight>
</lang>
<p>Another version:</p>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <mistral.h>
#include <mistral.h>


Line 407: Line 407:
"RANKINE : ", {temperatura} POR '1.8', NL)
"RANKINE : ", {temperatura} POR '1.8', NL)
FINALIZAR
FINALIZAR
</syntaxhighlight>
</lang>
<p>Or another (and last) version:</p>
<p>Or another (and last) version:</p>
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <mistral.h>
#include <mistral.h>


Line 421: Line 421:
"RANKINE : ", rankine, NL)
"RANKINE : ", rankine, NL)
FINALIZAR
FINALIZAR
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 440: Line 440:
=={{header|APL}}==
=={{header|APL}}==
Given a temperature in Kelvin, prints the equivalent in Kelvin, Celsius, Fahrenheit, and Rankine (in that order).
Given a temperature in Kelvin, prints the equivalent in Kelvin, Celsius, Fahrenheit, and Rankine (in that order).
<lang apl> CONVERT←{⍵,(⍵-273.15),(R-459.67),(R←⍵×9÷5)}</lang>
<syntaxhighlight lang="apl"> CONVERT←{⍵,(⍵-273.15),(R-459.67),(R←⍵×9÷5)}</syntaxhighlight>
{{out}}
{{out}}
<lang apl> CONVERT 21
<syntaxhighlight lang="apl"> CONVERT 21
21 ¯252.15 ¯421.87 37.8</lang>
21 ¯252.15 ¯421.87 37.8</syntaxhighlight>
The "high minus" character <tt>¯</tt> is used in APL to mark negative numbers, preventing any possible confusion with <tt>-</tt> (the subtraction operator).
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}}==
=={{header|AppleScript}}==
{{Trans|JavaScript}} ( ES6 version )
{{Trans|JavaScript}} ( ES6 version )
<lang AppleScript>use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function
<syntaxhighlight lang="applescript">use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function


-- KELVIN TO OTHER SCALE -----------------------------------------------------
-- KELVIN TO OTHER SCALE -----------------------------------------------------
Line 560: Line 560:
((ca's NSString's stringWithString:(str))'s ¬
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower</lang>
end toLower</syntaxhighlight>
{{Out}}
{{Out}}
<pre>K 21.0
<pre>K 21.0
Line 571: Line 571:
Or of course:
Or of course:


<lang applescript>on convertFromKelvin(kelvinValue)
<syntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
return ("K" & tab & (kelvinValue as real)) & ¬
return ("K" & tab & (kelvinValue as real)) & ¬
(linefeed & "C" & tab & (kelvinValue - 273.15)) & ¬
(linefeed & "C" & tab & (kelvinValue - 273.15)) & ¬
Line 578: Line 578:
end convertFromKelvin
end convertFromKelvin


convertFromKelvin(21)</lang>
convertFromKelvin(21)</syntaxhighlight>


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:
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:


<lang applescript>on convertFromKelvin(kelvinValue)
<syntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
set kelvinMeasurement to kelvinValue as degrees Kelvin
set kelvinMeasurement to kelvinValue as degrees Kelvin
Line 595: Line 595:
end convertFromKelvin
end convertFromKelvin


convertFromKelvin(21)</lang>
convertFromKelvin(21)</syntaxhighlight>


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.
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.


<lang applescript>use AppleScript version "2.5" -- macOS 10.12 (Sierra) or later
<syntaxhighlight lang="applescript">use AppleScript version "2.5" -- macOS 10.12 (Sierra) or later
use framework "Foundation"
use framework "Foundation"


Line 632: Line 632:
end convertFromKelvin
end convertFromKelvin


convertFromKelvin(21)</lang>
convertFromKelvin(21)</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>convertKelvins: function [k][
<syntaxhighlight lang="rebol">convertKelvins: function [k][
#[
#[
celcius: k - 273.15
celcius: k - 273.15
Line 644: Line 644:
]
]


print convertKelvins 100</lang>
print convertKelvins 100</syntaxhighlight>


{{out}}
{{out}}
Line 651: Line 651:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox, % "Kelvin:`t`t 21.00 K`n"
<syntaxhighlight lang="autohotkey">MsgBox, % "Kelvin:`t`t 21.00 K`n"
. "Celsius:`t`t" kelvinToCelsius(21) " C`n"
. "Celsius:`t`t" kelvinToCelsius(21) " C`n"
. "Fahrenheit:`t" kelvinToFahrenheit(21) " F`n"
. "Fahrenheit:`t" kelvinToFahrenheit(21) " F`n"
Line 667: Line 667:
{
{
return, round(k * 1.8, 2)
return, round(k * 1.8, 2)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Kelvin: 21.00 K
<pre>Kelvin: 21.00 K
Line 675: Line 675:


=={{header|AutoIt}}==
=={{header|AutoIt}}==
<lang AutoIt>; ### USAGE - TESTING PURPOSES ONLY
<syntaxhighlight lang="autoit">; ### USAGE - TESTING PURPOSES ONLY


Local Const $_KELVIN = 21
Local Const $_KELVIN = 21
Line 694: Line 694:
Return Round($degrees * 1.8, 2)
Return Round($degrees * 1.8, 2)
EndSelect
EndSelect
EndFunc ;==> Kelvin</lang>
EndFunc ;==> Kelvin</syntaxhighlight>
{{out}}
{{out}}
<pre>Kelvin: 21°
<pre>Kelvin: 21°
Line 703: Line 703:
=={{header|AWK}}==
=={{header|AWK}}==
"Interactive" version, reading from stdin only:
"Interactive" version, reading from stdin only:
<lang AWK># syntax: AWK -f TEMPERATURE_CONVERSION.AWK
<syntaxhighlight lang="awk"># syntax: AWK -f TEMPERATURE_CONVERSION.AWK
BEGIN {
BEGIN {
while (1) {
while (1) {
Line 721: Line 721:
}
}
exit(0)
exit(0)
}</lang>
}</syntaxhighlight>


"Regular" version, reading from input-file(s). <br>
"Regular" version, reading from input-file(s). <br>
Line 727: Line 727:


{{works with|gawk}} BEGINFILE is a gawk-extension
{{works with|gawk}} BEGINFILE is a gawk-extension
<lang AWK># usage: gawk -f temperature_conversion.awk input.txt -
<syntaxhighlight lang="awk"># usage: gawk -f temperature_conversion.awk input.txt -


BEGIN { print("# Temperature conversion\n") }
BEGIN { print("# Temperature conversion\n") }
Line 746: Line 746:


END { print("# Bye.") }
END { print("# Bye.") }
</syntaxhighlight>
</lang>


{{out|Input}} the numeric value of the first word in each line is used as input for the conversion
{{out|Input}} the numeric value of the first word in each line is used as input for the conversion
Line 820: Line 820:
=={{header|BASIC}}==
=={{header|BASIC}}==


<lang basic>
<syntaxhighlight lang="basic">
10 REM TRANSLATION OF AWK VERSION
10 REM TRANSLATION OF AWK VERSION
20 INPUT "KELVIN DEGREES",K
20 INPUT "KELVIN DEGREES",K
Line 832: Line 832:
100 PRINT R; " DEGREES RANKINE"
100 PRINT R; " DEGREES RANKINE"
110 GOTO 20
110 GOTO 20
</syntaxhighlight>
</lang>


==={{header|QBasic}}===
==={{header|QBasic}}===
<syntaxhighlight lang="qbasic">DO
<lang QBasic>DO
INPUT "Kelvin degrees (>=0): ", K
INPUT "Kelvin degrees (>=0): ", K
LOOP UNTIL K >= 0
LOOP UNTIL K >= 0
Line 843: Line 843:
PRINT "F = " + STR$(K * 1.8 - 459.67)
PRINT "F = " + STR$(K * 1.8 - 459.67)
PRINT "R = " + STR$(K * 1.8)
PRINT "R = " + STR$(K * 1.8)
END</lang>
END</syntaxhighlight>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
{{works with|QBasic}}
{{works with|QBasic}}
<lang qbasic>DO
<syntaxhighlight lang="qbasic">DO
PRINT "Kelvin degrees (>=0): ";
PRINT "Kelvin degrees (>=0): ";
INPUT K
INPUT K
Line 856: Line 856:
PRINT "F = "; STR$(K * 1.8 - 459.67)
PRINT "F = "; STR$(K * 1.8 - 459.67)
PRINT "R = "; STR$(K * 1.8)
PRINT "R = "; STR$(K * 1.8)
END</lang>
END</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>repeat
<syntaxhighlight lang="yabasic">repeat
input "Kelvin degrees (>=0): " K
input "Kelvin degrees (>=0): " K
until K >= 0
until K >= 0
Line 867: Line 867:
print "F = " + str$(K * 1.8 - 459.67)
print "F = " + str$(K * 1.8 - 459.67)
print "R = " + str$(K * 1.8)
print "R = " + str$(K * 1.8)
end</lang>
end</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 INPUT PROMPT "Kelvin degrees: ":K
<syntaxhighlight lang="is-basic">100 INPUT PROMPT "Kelvin degrees: ":K
110 PRINT K;TAB(10);"Kelvin is equivalent to"
110 PRINT K;TAB(10);"Kelvin is equivalent to"
120 PRINT K-273.15;TAB(10);"Degrees Celsius"
120 PRINT K-273.15;TAB(10);"Degrees Celsius"
130 PRINT K*1.8-459.67;TAB(10);"Degrees Fahrenheit"
130 PRINT K*1.8-459.67;TAB(10);"Degrees Fahrenheit"
140 PRINT K*1.8;TAB(10);"Degrees Rankine"</lang>
140 PRINT K*1.8;TAB(10);"Degrees Rankine"</syntaxhighlight>


==={{header|Sinclair ZX81 BASIC}}===
==={{header|Sinclair ZX81 BASIC}}===
<lang basic>10 PRINT "ENTER A TEMPERATURE IN KELVINS"
<syntaxhighlight lang="basic">10 PRINT "ENTER A TEMPERATURE IN KELVINS"
20 INPUT K
20 INPUT K
30 PRINT K;" KELVINS ="
30 PRINT K;" KELVINS ="
40 PRINT K-273.15;" DEGREES CELSIUS"
40 PRINT K-273.15;" DEGREES CELSIUS"
50 PRINT K*1.8-459.67;" DEGREES FAHRENHEIT"
50 PRINT K*1.8-459.67;" DEGREES FAHRENHEIT"
60 PRINT K*1.8;" DEGREES RANKINE"</lang>
60 PRINT K*1.8;" DEGREES RANKINE"</syntaxhighlight>


==={{Header|Tiny BASIC}}===
==={{Header|Tiny BASIC}}===
<lang tiny basic>
<syntaxhighlight lang="tiny basic">
PRINT "Temperature in Kelvin?"
PRINT "Temperature in Kelvin?"
INPUT K
INPUT K
Line 893: Line 893:
PRINT C," Celsius"
PRINT C," Celsius"
PRINT F," Fahrenheit"
PRINT F," Fahrenheit"
PRINT R," Rankine"</lang>
PRINT R," Rankine"</syntaxhighlight>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang basic256>
<syntaxhighlight lang="basic256">
do
do
print "Kelvin degrees (>=0): ";
print "Kelvin degrees (>=0): ";
Line 906: Line 906:
print "F = " + string(K * 1.8 - 459.67)
print "F = " + string(K * 1.8 - 459.67)
print "R = " + string(K * 1.8)
print "R = " + string(K * 1.8)
</syntaxhighlight>
</lang>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic>
<syntaxhighlight lang="bbcbasic">
REPEAT
REPEAT
INPUT "Kelvin degrees (>=0): " K
INPUT "Kelvin degrees (>=0): " K
Line 919: Line 919:
PRINT "R = " K * 1.8
PRINT "R = " K * 1.8
END
END
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 933: 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.
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.


<lang befunge>0000>0p~>"."-:!#v_2-::0\`\9`+!#v_$1>/\:3`#v_\>\:3 \`#v_v
<syntaxhighlight 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_
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
"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:,<
v/+55\+*86%+55: /+55\+*86%+55: \0/+55+5*-\1*2 p00:`\0:,<
>"."\>:55+% 68*v >:#,_$55+,\:!#@_^
>"."\>:55+% 68*v >:#,_$55+,\:!#@_^
$_^#!:/+55\+< ^\" :"_<g00*95 </lang>
$_^#!:/+55\+< ^\" :"_<g00*95 </syntaxhighlight>


{{out}}
{{out}}
Line 948: Line 948:


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>( ( rational2fixedpoint
<syntaxhighlight lang="bracmat">( ( rational2fixedpoint
= minus fixedpointnumber number decimals
= minus fixedpointnumber number decimals
. !arg:(#?number.~<0:~/#?decimals)
. !arg:(#?number.~<0:~/#?decimals)
Line 993: Line 993:
)
)
& done!
& done!
)</lang>
)</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter Kelvin temperature:21.00
<pre>Enter Kelvin temperature:21.00
Line 1,003: Line 1,003:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 1,031: Line 1,031:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace TemperatureConversion
namespace TemperatureConversion
Line 1,064: Line 1,064:
}
}
}
}
}</lang>
}</syntaxhighlight>


<pre>
<pre>
Line 1,075: Line 1,075:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>
#include <iomanip>
#include <iomanip>
Line 1,130: Line 1,130:
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,149: Line 1,149:


=={{header|Ceylon}}==
=={{header|Ceylon}}==
<lang ceylon>shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {


void printKelvinConversions(Float kelvin) {
void printKelvinConversions(Float kelvin) {
Line 1,164: Line 1,164:
printKelvinConversions(21.0);
printKelvinConversions(21.0);
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Common Lisp}}
{{trans|Common Lisp}}
<lang clojure>(defn to-celsius [k]
<syntaxhighlight lang="clojure">(defn to-celsius [k]
(- k 273.15))
(- k 273.15))
(defn to-fahrenheit [k]
(defn to-fahrenheit [k]
Line 1,179: Line 1,179:
(format "Celsius: %.2f Fahrenheit: %.2f Rankine: %.2f"
(format "Celsius: %.2f Fahrenheit: %.2f Rankine: %.2f"
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format "Error: Non-numeric value entered.")))</lang>
(format "Error: Non-numeric value entered.")))</syntaxhighlight>


{{out}}
{{out}}
Line 1,188: Line 1,188:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>kelvin = proc (k: real) returns (real)
<syntaxhighlight lang="clu">kelvin = proc (k: real) returns (real)
return(k)
return(k)
end kelvin
end kelvin
Line 1,225: Line 1,225:
stream$putl(po, f_form(c.func(k), 6, 2))
stream$putl(po, f_form(c.func(k), 6, 2))
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter temperature in Kelvin: 21
<pre>Enter temperature in Kelvin: 21
Line 1,235: Line 1,235:
=={{header|COBOL}}==
=={{header|COBOL}}==
{{works with|Visual COBOL}}
{{works with|Visual COBOL}}
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. temp-conversion.
PROGRAM-ID. temp-conversion.
Line 1,270: Line 1,270:
GOBACK
GOBACK
.</lang>
.</syntaxhighlight>


{{out}}
{{out}}
Line 1,284: Line 1,284:
Three functions define the necessary conversion formulas. A fancy format string is used to print these values.
Three functions define the necessary conversion formulas. A fancy format string is used to print these values.


<lang lisp>
<syntaxhighlight lang="lisp">
(defun to-celsius (k)
(defun to-celsius (k)
(- k 273.15))
(- k 273.15))
Line 1,298: Line 1,298:
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format t "Error: Non-numeric value entered."))))
(format t "Error: Non-numeric value entered."))))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,311: Line 1,311:


=={{header|D}}==
=={{header|D}}==
<lang d>double kelvinToCelsius(in double k) pure nothrow @safe {
<syntaxhighlight lang="d">double kelvinToCelsius(in double k) pure nothrow @safe {
return k - 273.15;
return k - 273.15;
}
}
Line 1,343: Line 1,343:
writefln("%2.2f K is below absolute zero", kelvin);
writefln("%2.2f K is below absolute zero", kelvin);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,356: Line 1,356:


=={{header|Delphi}}==
=={{header|Delphi}}==
<lang delphi>
<syntaxhighlight lang="delphi">
program Temperature;
program Temperature;


Line 1,398: Line 1,398:
readln;
readln;
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,409: Line 1,409:
=={{header|EasyLang}}==
=={{header|EasyLang}}==


<lang>k = number input
<syntaxhighlight lang="text">k = number input
print k & " °K"
print k & " °K"
print k - 273.15 & " °C"
print k - 273.15 & " °C"
print k * 1.8 - 459.67 & " °F"
print k * 1.8 - 459.67 & " °F"
print k * 1.8 & " °R"</lang>
print k * 1.8 & " °R"</syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.1 :
ELENA 4.1 :
<lang elena>import extensions;
<syntaxhighlight lang="elena">import extensions;
convertKelvinToFahrenheit(x)
convertKelvinToFahrenheit(x)
Line 1,449: Line 1,449:
console.printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp));
console.printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp));
console.readChar()
console.readChar()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,460: Line 1,460:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Temperature do
<syntaxhighlight lang="elixir">defmodule Temperature do
def conversion(t) do
def conversion(t) do
IO.puts "K : #{f(t)}"
IO.puts "K : #{f(t)}"
Line 1,475: Line 1,475:
end
end


Temperature.task</lang>
Temperature.task</syntaxhighlight>


{{out}}
{{out}}
Line 1,489: Line 1,489:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>% Implemented by Arjun Sunel
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(temp_conv).
-module(temp_conv).
-export([main/0]).
-export([main/0]).
Line 1,504: Line 1,504:
f(A) ->
f(A) ->
(round(A*100))/100 .
(round(A*100))/100 .
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,519: Line 1,519:


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<syntaxhighlight lang="openeuphoria">
<lang OpenEuphoria>
include std/console.e
include std/console.e


Line 1,527: 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})
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
end while
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,540: Line 1,540:


=={{header|Excel}}==
=={{header|Excel}}==
<lang>A1 : Kelvin
<syntaxhighlight lang="text">A1 : Kelvin
B1 : Celsius
B1 : Celsius
C1 : Fahrenheit
C1 : Fahrenheit
Line 1,548: Line 1,548:
C2 : =K*1.8-459.67
C2 : =K*1.8-459.67
D2 : =K*1.8
D2 : =K*1.8
Input in A1 </lang>
Input in A1 </syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,565: Line 1,565:


{{Works with|Office 365 betas 2021}}
{{Works with|Office 365 betas 2021}}
<lang lisp>FROMKELVIN
<syntaxhighlight lang="lisp">FROMKELVIN
=LAMBDA(toUnit,
=LAMBDA(toUnit,
LAMBDA(n,
LAMBDA(n,
Line 1,580: Line 1,580:
)
)
)
)
)</lang>
)</syntaxhighlight>


The example below generates the spaced list of test values on the left from the expression ENUMFROMTHENTO(240)(250)(390),
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:
applying the following custom function:
<lang lisp>ENUMFROMTHENTO
<syntaxhighlight lang="lisp">ENUMFROMTHENTO
=LAMBDA(a,
=LAMBDA(a,
LAMBDA(b,
LAMBDA(b,
Line 1,598: Line 1,598:
)
)
)
)
)</lang>
)</syntaxhighlight>


The four columns on the right of the output read their target format from the label cell at the top of each column.
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: Line 1,737:


=={{header|Ezhil}}==
=={{header|Ezhil}}==
<syntaxhighlight lang="ezhil">
<lang Ezhil>
# convert from Kelvin
# convert from Kelvin
நிரல்பாகம் கெல்வின்_இருந்து_மாற்று( k )
நிரல்பாகம் கெல்வின்_இருந்து_மாற்று( k )
Line 1,746: Line 1,746:
கெல்வின்_இருந்து_மாற்று( 273 ) #freezing pt of water
கெல்வின்_இருந்து_மாற்று( 273 ) #freezing pt of water
கெல்வின்_இருந்து_மாற்று( 30 + 273 ) #room temperature in Summer
கெல்வின்_இருந்து_மாற்று( 30 + 273 ) #room temperature in Summer
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Define units of measure
// Define units of measure
[<Measure>] type k
[<Measure>] type k
Line 1,766: Line 1,766:
printfn "%A Kelvin is %A Fahrenheit" K (kelvinToFahrenheit K)
printfn "%A Kelvin is %A Fahrenheit" K (kelvinToFahrenheit K)
printfn "%A Kelvin is %A Rankine" K (kelvinToRankine K)
printfn "%A Kelvin is %A Rankine" K (kelvinToRankine K)
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang>USING: combinators formatting kernel math ;
<syntaxhighlight lang="text">USING: combinators formatting kernel math ;
IN: rosetta-code.temperature
IN: rosetta-code.temperature


Line 1,780: Line 1,780:
"K %.2f\nC %.2f\nF %.2f\nR %.2f\n" printf ;
"K %.2f\nC %.2f\nF %.2f\nR %.2f\n" printf ;
21 convert</lang>
21 convert</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,790: Line 1,790:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang focal>01.10 ASK "TEMPERATURE IN KELVIN", K
<syntaxhighlight lang="focal">01.10 ASK "TEMPERATURE IN KELVIN", K
01.20 TYPE "K ", %6.02, K, !
01.20 TYPE "K ", %6.02, K, !
01.30 TYPE "C ", %6.02, K - 273.15, !
01.30 TYPE "C ", %6.02, K - 273.15, !
01.40 TYPE "F ", %6.02, K * 1.8 - 459.67, !
01.40 TYPE "F ", %6.02, K * 1.8 - 459.67, !
01.50 TYPE "R ", %6.02, K * 1.8, !</lang>
01.50 TYPE "R ", %6.02, K * 1.8, !</syntaxhighlight>
{{out}}
{{out}}
<pre>TEMPERATURE IN KELVIN:373.15
<pre>TEMPERATURE IN KELVIN:373.15
Line 1,804: Line 1,804:
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|GNU Forth}} for the command line handling
{{works with|GNU Forth}} for the command line handling
<lang forth>: k>°C ( F: kelvin -- celsius ) 273.15e0 f- ;
<syntaxhighlight lang="forth">: k>°C ( F: kelvin -- celsius ) 273.15e0 f- ;
: k>°R ( F: kelvin -- rankine ) 1.8e0 f* ;
: k>°R ( F: kelvin -- rankine ) 1.8e0 f* ;
: °R>°F ( F: rankine -- fahrenheit ) 459.67e0 f- ;
: °R>°F ( F: rankine -- fahrenheit ) 459.67e0 f- ;
Line 1,816: Line 1,816:
then ;
then ;


main bye</lang>
main bye</syntaxhighlight>
{{out}}
{{out}}
<pre>&gt; gforthamd64 rosetta_temp_conv.fs 21
<pre>&gt; gforthamd64 rosetta_temp_conv.fs 21
Line 1,826: Line 1,826:
=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran>Program Temperature
<syntaxhighlight lang="fortran">Program Temperature
implicit none
implicit none
Line 1,851: Line 1,851:


end subroutine
end subroutine
end program</lang>
end program</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Sub convKelvin(temp As Double)
Sub convKelvin(temp As Double)
Line 1,873: Line 1,873:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 1,889: Line 1,889:


=={{header|Gambas}}==
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim fKelvin As Float
Dim fKelvin As Float


Line 1,899: Line 1,899:
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,909: Line 1,909:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,935: Line 1,935:
fmt.Printf("F %.2f\n", k*9/5-459.67)
fmt.Printf("F %.2f\n", k*9/5-459.67)
fmt.Printf("R %.2f\n", k*9/5)
fmt.Printf("R %.2f\n", k*9/5)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,946: Line 1,946:


=={{header|Groovy}}==
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
class Convert{
class Convert{
static void main(String[] args){
static void main(String[] args){
Line 1,959: Line 1,959:
static def k_to_r(def k=21.0){return k*1.8;}
static def k_to_r(def k=21.0){return k*1.8;}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,970: Line 1,970:
=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell>import System.Exit (die)
<syntaxhighlight lang="haskell">import System.Exit (die)
import Control.Monad (mapM_)
import Control.Monad (mapM_)


Line 1,985: Line 1,985:
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (*1.8)]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (*1.8)]
nums = (show . ($n)) <$> conversions</lang>
nums = (show . ($n)) <$> conversions</syntaxhighlight>


Or with properly managed exceptions:
Or with properly managed exceptions:


<lang haskell>{-# LANGUAGE LambdaCase #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE LambdaCase #-}


import System.Exit (die)
import System.Exit (die)
Line 2,012: Line 2,012:
t <- liftIO getLine >>= tryRead "Could not read temp"
t <- liftIO getLine >>= tryRead "Could not read temp"
tryAssert "Temp cannot be negative" (t>=0)
tryAssert "Temp cannot be negative" (t>=0)
return t</lang>
return t</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==


The following program works in both languages:
The following program works in both languages:
<lang unicon>procedure main(A)
<syntaxhighlight lang="unicon">procedure main(A)
k := A[1] | 21.00
k := A[1] | 21.00
write("K ",k)
write("K ",k)
Line 2,023: Line 2,023:
write("R ",r := k*(9.0/5.0))
write("R ",r := k*(9.0/5.0))
write("F ",r - 459.67)
write("F ",r - 459.67)
end</lang>
end</syntaxhighlight>


Sample runs:
Sample runs:
Line 2,042: Line 2,042:


=={{header|J}}==
=={{header|J}}==
'''Solution''':<lang j> NB. Temp conversions are all linear polynomials
'''Solution''':<syntaxhighlight lang="j"> NB. Temp conversions are all linear polynomials
K2K =: 0 1 NB. K = (1 *k) + 0
K2K =: 0 1 NB. K = (1 *k) + 0
K2C =: _273 1 NB. C = (1 *k) - 273
K2C =: _273 1 NB. C = (1 *k) - 273
Line 2,052: Line 2,052:
NB. numeric matrix J programs would manipulate
NB. numeric matrix J programs would manipulate
NB. directly.
NB. directly.
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</lang>
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</syntaxhighlight>
{{out|Example}}
{{out|Example}}
<lang j> NB. Format matrix for printing & tag each
<syntaxhighlight lang="j"> NB. Format matrix for printing & tag each
NB. temp with scale, for human legibility
NB. temp with scale, for human legibility
fmt =: [: (;:inv"1) 0 _1 |: 'KCFR' ;"0 1"_1 '0.2' 8!:0 ]
fmt =: [: (;:inv"1) 0 _1 |: 'KCFR' ;"0 1"_1 '0.2' 8!:0 ]
Line 2,075: Line 2,075:
C -252.00 -173.00 27.00
C -252.00 -173.00 27.00
F -421.87 -279.67 80.33
F -421.87 -279.67 80.33
R 37.80 180.00 540.00</lang>
R 37.80 180.00 540.00</syntaxhighlight>
'''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).
'''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}}==
=={{header|Java}}==
<lang java>public class TemperatureConversion {
<syntaxhighlight lang="java">public class TemperatureConversion {
public static void main(String args[]) {
public static void main(String args[]) {
if (args.length == 1) {
if (args.length == 1) {
Line 2,109: Line 2,109:
return k * 1.8;
return k * 1.8;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,124: Line 2,124:


===ES5===
===ES5===
<lang javascript>var k2c = k => k - 273.15
<syntaxhighlight lang="javascript">var k2c = k => k - 273.15
var k2r = k => k * 1.8
var k2r = k => k * 1.8
var k2f = k => k2r(k) - 459.67
var k2f = k => k2r(k) - 459.67
Line 2,137: Line 2,137:
kCnv(21)
kCnv(21)
kCnv(295)</lang>
kCnv(295)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,148: Line 2,148:
Deriving '''kelvinTranslations()''' from a more general '''heatBabel()''' function.
Deriving '''kelvinTranslations()''' from a more general '''heatBabel()''' function.


<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 2,186: Line 2,186:


})();
})();
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 2,196: Line 2,196:


=={{header|jq}}==
=={{header|jq}}==
The hard part here is defining round/1 generically.<lang jq>
The hard part here is defining round/1 generically.<syntaxhighlight lang="jq">
# round(keep) takes as input any jq (i.e. JSON) number and emits a string.
# 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,
# "keep" is the desired maximum number of numerals after the decimal point,
Line 2,233: Line 2,233:
end;
end;


cfr</lang>
cfr</syntaxhighlight>
'''Example'''
'''Example'''
<lang sh> $ jq -M -r -f Temperature_conversion.jq
<syntaxhighlight lang="sh"> $ jq -M -r -f Temperature_conversion.jq
21
21
Kelvin: 21
Kelvin: 21
Line 2,243: Line 2,243:
-1
-1
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin</lang>
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>cfr(k) = print("Kelvin: $k, ",
<syntaxhighlight lang="julia">cfr(k) = print("Kelvin: $k, ",
"Celsius: $(round(k-273.15,2)), ",
"Celsius: $(round(k-273.15,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Rankine: $(round(k*1.8,2))")</lang>
"Rankine: $(round(k*1.8,2))")</syntaxhighlight>
<pre>julia> cfr(21)
<pre>julia> cfr(21)
Kelvin: 21, Celsius: -252.15, Fahrenheit: -421.87, Rankine: 37.8</pre>
Kelvin: 21, Celsius: -252.15, Fahrenheit: -421.87, Rankine: 37.8</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


class Kelvin(val degrees: Double) {
class Kelvin(val degrees: Double) {
Line 2,274: Line 2,274:
println("F ${f.format(k.toFahreneit())}\n")
println("F ${f.format(k.toFahreneit())}\n")
println("R ${f.format(k.toRankine())}")
println("R ${f.format(k.toRankine())}")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,290: Line 2,290:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{def to-celsius {lambda {:k} {- :k 273.15}}}
{def to-celsius {lambda {:k} {- :k 273.15}}}
-> to-celsius
-> to-celsius
Line 2,317: Line 2,317:
-421.87 farenheit
-421.87 farenheit
37.8 rankine
37.8 rankine
</syntaxhighlight>
</lang>


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>define tempconverter(temp, kind) => {
<syntaxhighlight lang="lasso">define tempconverter(temp, kind) => {


local(
local(
Line 2,370: Line 2,370:
tempconverter(37.80, 'r')
tempconverter(37.80, 'r')
'<br />'
'<br />'
tempconverter(69.80, 'f')</lang>
tempconverter(69.80, 'f')</syntaxhighlight>
<pre>K = 21.00 C = -252.15 R = 37.80 F = -421.87
<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
K = 294.15 C = 21.00 R = 529.47 F = 69.80
Line 2,378: Line 2,378:


=={{header|LIL}}==
=={{header|LIL}}==
<lang tcl># Temperature conversion, in LIL
<syntaxhighlight lang="tcl"># Temperature conversion, in LIL
func kToc k {expr $k - 273.15}
func kToc k {expr $k - 273.15}
func kTor k {expr $k / 5.0 * 9.0}
func kTor k {expr $k / 5.0 * 9.0}
Line 2,389: Line 2,389:
print "Fahrenheit: [kTof $k]"
print "Fahrenheit: [kTof $k]"
print "Rankine: [kTor $k]"
print "Rankine: [kTor $k]"
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,400: Line 2,400:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>function convertDegrees k
<syntaxhighlight lang="livecode">function convertDegrees k
put k/5 * 9 into r
put k/5 * 9 into r
put k - 273.15 into c
put k - 273.15 into c
put r - 459.67 into f
put r - 459.67 into f
return k,r,c,f
return k,r,c,f
end convertDegrees</lang>
end convertDegrees</syntaxhighlight>
Example<lang LiveCode>put convertDegrees(21.00) into tTemp
Example<syntaxhighlight lang="livecode">put convertDegrees(21.00) into tTemp
put item 1 of tTemp into temperature["Kelvin"]
put item 1 of tTemp into temperature["Kelvin"]
put item 2 of tTemp into temperature["Rankine"]
put item 2 of tTemp into temperature["Rankine"]
Line 2,414: Line 2,414:
put temperature
put temperature


-- Celsius:-252.15,Fahrenheit:-421.87,Kelvin:21.00,Rankine:37.8</lang>
-- Celsius:-252.15,Fahrenheit:-421.87,Kelvin:21.00,Rankine:37.8</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==


<lang lua>function convert_temp(k)
<syntaxhighlight lang="lua">function convert_temp(k)
local c = k - 273.15
local c = k - 273.15
local r = k * 1.8
local r = k * 1.8
Line 2,430: Line 2,430:
Rankine: %.2f °R
Rankine: %.2f °R
Fahrenheit: %.2f °F
Fahrenheit: %.2f °F
]],convert_temp(21.0)))</lang>
]],convert_temp(21.0)))</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==


<syntaxhighlight lang="liberty basic">Do
<lang Liberty BASIC>Do
Input "Kelvin degrees (>=0): ";K
Input "Kelvin degrees (>=0): ";K
Loop Until (K >= 0)
Loop Until (K >= 0)
Line 2,442: Line 2,442:
Print "F = ";(K * 1.8 - 459.67)
Print "F = ";(K * 1.8 - 459.67)
Print "R = ";(K * 1.8)
Print "R = ";(K * 1.8)
End</lang>
End</syntaxhighlight>


{{out}}
{{out}}
Line 2,453: Line 2,453:


=={{header|Maple}}==
=={{header|Maple}}==
<lang maple>tempConvert := proc(k)
<syntaxhighlight 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]);
seq(printf("%c: %.2f\n", StringTools[UpperCase](substring(i, 1)), convert(k, temperature, kelvin, i)), i in [kelvin, Celsius, Fahrenheit, Rankine]);
return NULL;
return NULL;
end proc:
end proc:


tempConvert(21);</lang>
tempConvert(21);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,468: Line 2,468:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>tempConvert[t_] := # -> Thread@UnitConvert[#,{"DegreesFahrenheit", "DegreesCelsius", "DegreesRankine"}]&@Quantity[N@t, "Kelvins"]
<syntaxhighlight lang="mathematica">tempConvert[t_] := # -> Thread@UnitConvert[#,{"DegreesFahrenheit", "DegreesCelsius", "DegreesRankine"}]&@Quantity[N@t, "Kelvins"]
tempConvert[21]</lang>
tempConvert[21]</syntaxhighlight>
{{out}}
{{out}}
<pre>21.K -> {-421.87°F,-252.15°C,37.8°R}</pre>
<pre>21.K -> {-421.87°F,-252.15°C,37.8°R}</pre>
Line 2,475: Line 2,475:
=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.3}}
{{works with|min|0.19.3}}
<lang min>(
<syntaxhighlight lang="min">(
((float) (273.15 -) (9 5 / * 459.67 -) (9 5 / *)) cleave
((float) (273.15 -) (9 5 / * 459.67 -) (9 5 / *)) cleave
() 'cons 4 times "K $1\nC $2\nF $3\nR $4" swap % puts!
() 'cons 4 times "K $1\nC $2\nF $3\nR $4" swap % puts!
) :convert
) :convert


21 convert</lang>
21 convert</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,490: Line 2,490:


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>fromKelvin = function(temp)
<syntaxhighlight lang="miniscript">fromKelvin = function(temp)
print temp + " degrees in Kelvin is:"
print temp + " degrees in Kelvin is:"
Celsius = temp - 273.15
Celsius = temp - 273.15
Line 2,501: Line 2,501:


temp = input("Enter a temperature in Kelvin: ")
temp = input("Enter a temperature in Kelvin: ")
fromKelvin temp.val</lang>
fromKelvin temp.val</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,518: Line 2,518:


=={{header|MiniZinc}}==
=={{header|MiniZinc}}==
<lang MiniZinc>float: kelvin;
<syntaxhighlight lang="minizinc">float: kelvin;


var float: celsius;
var float: celsius;
Line 2,529: Line 2,529:
solve satisfy;
solve satisfy;


output ["K \(kelvin)\n", "C \(celsius)\n", "F \(fahrenheit)\n", "R \(rankine)\n"];</lang>
output ["K \(kelvin)\n", "C \(celsius)\n", "F \(fahrenheit)\n", "R \(rankine)\n"];</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
Compiling temperature.mzn, additional arguments kelvin=1000;
Compiling temperature.mzn, additional arguments kelvin=1000;
Line 2,542: Line 2,542:


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
<lang mk61>П7 0 , 8 * П8 ИП7 9 * 5
<syntaxhighlight lang="mk61">П7 0 , 8 * П8 ИП7 9 * 5
/ 3 2 + П9 ИП7 2 7 3 ,
/ 3 2 + П9 ИП7 2 7 3 ,
1 5 + П4 С/П П8 1 , 8 /
1 5 + П4 С/П П8 1 , 8 /
БП 00 П9 3 2 - 5 * 9 /
БП 00 П9 3 2 - 5 * 9 /
БП 00 П4 2 7 3 , 1 5 -
БП 00 П4 2 7 3 , 1 5 -
БП 00</lang>
БП 00</syntaxhighlight>


''Instruction:''
''Instruction:''
Line 2,572: Line 2,572:
==={{header|mLite}}===
==={{header|mLite}}===
Temperature in Kelvin given on command line.
Temperature in Kelvin given on command line.
<lang ocaml>fun KtoC n = n - 273.15;
<syntaxhighlight lang="ocaml">fun KtoC n = n - 273.15;
fun KtoF n = n * 1.8 - 459.67;
fun KtoF n = n * 1.8 - 459.67;
fun KtoR n = n * 1.8;
fun KtoR n = n * 1.8;
Line 2,588: Line 2,588:
print "Rankine: "; println ` KtoR K
print "Rankine: "; println ` KtoR K
end
end
</syntaxhighlight>
</lang>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
{{trans|Python}}
{{trans|Python}}
<lang Nanoquery>% while true
<syntaxhighlight lang="nanoquery">% while true
... print "K ? "
... print "K ? "
... k = float(input())
... k = float(input())
Line 2,602: Line 2,602:
K ? 222.2
K ? 222.2
222.200 Kelvin = -50.9500 Celsius = -59.7100 Fahrenheit = 399.960 Rankine degrees.
222.200 Kelvin = -50.9500 Celsius = -59.7100 Fahrenheit = 399.960 Rankine degrees.
K ? </lang>
K ? </syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols
options replace format comments java crossref symbols


Line 2,790: Line 2,790:


return
return
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre pre style="height: 40ex; overflow: scroll">
<pre pre style="height: 40ex; overflow: scroll">
Line 2,855: Line 2,855:


=={{header|Never}}==
=={{header|Never}}==
<syntaxhighlight lang="never">
<lang Never>
func KtoC(k : float) -> float { k - 273.15 }
func KtoC(k : float) -> float { k - 273.15 }
func KtoF(k : float) -> float { k * 1.8 - 459.67 }
func KtoF(k : float) -> float { k * 1.8 - 459.67 }
Line 2,872: Line 2,872:
0
0
}
}
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 2,882: Line 2,882:


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(define (to-celsius k)
(define (to-celsius k)
(- k 273.15)
(- k 273.15)
Line 2,904: Line 2,904:
)
)
)
)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,915: Line 2,915:
=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|strfmt}}
{{libheader|strfmt}}
<lang nim>import rdstdin, strutils, strfmt
<syntaxhighlight lang="nim">import rdstdin, strutils, strfmt


while true:
while true:
let k = parseFloat readLineFromStdin "K ? "
let k = parseFloat readLineFromStdin "K ? "
echo "{:g} Kelvin = {:g} Celsius = {:g} Fahrenheit = {:g} Rankine degrees".fmt(
echo "{:g} Kelvin = {:g} Celsius = {:g} Fahrenheit = {:g} Rankine degrees".fmt(
k, k - 273.15, k * 1.8 - 459.67, k * 1.8)</lang>
k, k - 273.15, k * 1.8 - 459.67, k * 1.8)</syntaxhighlight>
Sample usage:
Sample usage:
<pre>K ? 21.0
<pre>K ? 21.0
Line 2,928: Line 2,928:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<syntaxhighlight lang="objeck">
class Temperature {
class Temperature {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
Line 2,954: Line 2,954:
}
}
}
}
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 2,964: Line 2,964:


=={{header|Objective-C}}==
=={{header|Objective-C}}==
<lang objc>#import <Foundation/Foundation.h>
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h>


int main(int argc, const char * argv[])
int main(int argc, const char * argv[])
Line 2,982: Line 2,982:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml>
<syntaxhighlight lang="ocaml">
let print_temp s t =
let print_temp s t =
print_string s;
print_string s;
Line 3,006: Line 3,006:
print_temp "F " (kelvin_to_fahrenheit k);
print_temp "F " (kelvin_to_fahrenheit k);
print_temp "R " (kelvin_to_rankine k);;
print_temp "R " (kelvin_to_rankine k);;
</syntaxhighlight>
</lang>


Sample session:
Sample session:
Line 3,020: Line 3,020:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>: kelvinToCelsius 273.15 - ;
<syntaxhighlight lang="oforth">: kelvinToCelsius 273.15 - ;
: kelvinToFahrenheit 1.8 * 459.67 - ;
: kelvinToFahrenheit 1.8 * 459.67 - ;
: kelvinToRankine 1.8 * ;
: kelvinToRankine 1.8 * ;
Line 3,027: Line 3,027:
n kelvinToCelsius println
n kelvinToCelsius println
n kelvinToFahrenheit println
n kelvinToFahrenheit println
n kelvinToRankine println ;</lang>
n kelvinToRankine println ;</syntaxhighlight>


{{out}}
{{out}}
Line 3,038: Line 3,038:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>f(x)=[x,x-273.15,1.8*x-459.67,1.8*x]</lang>
<syntaxhighlight lang="parigp">f(x)=[x,x-273.15,1.8*x-459.67,1.8*x]</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang Pascal>program TemperatureConvert;
<syntaxhighlight lang="pascal">program TemperatureConvert;


type
type
Line 3,094: Line 3,094:
writeln(' ', ConvertTemperature(kelvin, K, F) : 3 : 2, ' in degrees Fahrenheit.');
writeln(' ', ConvertTemperature(kelvin, K, F) : 3 : 2, ' in degrees Fahrenheit.');
writeln(' ', ConvertTemperature(kelvin, K, R) : 3 : 2, ' in degrees Rankine.');
writeln(' ', ConvertTemperature(kelvin, K, R) : 3 : 2, ' in degrees Rankine.');
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 3,106: Line 3,106:


=={{header|Perl}}==
=={{header|Perl}}==
<lang Perl>my %scale = (
<syntaxhighlight lang="perl">my %scale = (
Celcius => { factor => 1 , offset => -273.15 },
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Rankine => { factor => 1.8, offset => 0 },
Line 3,118: Line 3,118:
foreach (sort keys %scale) {
foreach (sort keys %scale) {
printf "%12s:%8.2f\n", $_, $kelvin * $scale{$_}{factor} + $scale{$_}{offset};
printf "%12s:%8.2f\n", $_, $kelvin * $scale{$_}{factor} + $scale{$_}{offset};
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,129: Line 3,129:
=={{header|Phix}}==
=={{header|Phix}}==
Modified copy of [[Temperature_conversion#Euphoria|Euphoria]]
Modified copy of [[Temperature_conversion#Euphoria|Euphoria]]
<!--<lang Phix>-->
<!--<syntaxhighlight lang="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: #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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 3,144: Line 3,144:


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>
<syntaxhighlight lang="php">


while (true) {
while (true) {
Line 3,163: Line 3,163:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter a value in kelvin (q to quit): 21
<pre>Enter a value in kelvin (q to quit): 21
Line 3,175: Line 3,175:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(scl 2)
<syntaxhighlight lang="picolisp">(scl 2)


(de convertKelvin (Kelvin)
(de convertKelvin (Kelvin)
Line 3,186: Line 3,186:
(tab (-3 8)
(tab (-3 8)
(car X)
(car X)
(format ((cdr X) Kelvin) *Scl) ) ) )</lang>
(format ((cdr X) Kelvin) *Scl) ) ) )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(convertKelvin 21.0)</lang>
<syntaxhighlight lang="picolisp">(convertKelvin 21.0)</syntaxhighlight>
{{out}}
{{out}}
<pre>K 21.00
<pre>K 21.00
Line 3,196: Line 3,196:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>*process source attributes xref;
<syntaxhighlight lang="pli">*process source attributes xref;
/* PL/I **************************************************************
/* PL/I **************************************************************
* 15.08.2013 Walter Pachl translated from NetRexx
* 15.08.2013 Walter Pachl translated from NetRexx
Line 3,343: Line 3,343:
End;
End;
End;
End;
End;</lang>
End;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,359: Line 3,359:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Put 21 into a kelvin temperature.
Put 21 into a kelvin temperature.
Line 3,392: Line 3,392:
Show the celsius temperature given "C".
Show the celsius temperature given "C".
Show the fahrenheit temperature given "F".
Show the fahrenheit temperature given "F".
Show the rankine temperature given "R".</lang>
Show the rankine temperature given "R".</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,403: Line 3,403:
=={{header|PowerShell}}==
=={{header|PowerShell}}==
{{trans|Tcl}}
{{trans|Tcl}}
<lang powershell>function temp($k){
<syntaxhighlight lang="powershell">function temp($k){
try{
try{
$c = $k - 273.15
$c = $k - 273.15
Line 3,423: Line 3,423:


$input=Read-host "Enter a temperature in Kelvin"
$input=Read-host "Enter a temperature in Kelvin"
temp $input</lang>
temp $input</syntaxhighlight>
{{Out}}
{{Out}}
<pre>PS> ./TEMPS
<pre>PS> ./TEMPS
Line 3,437: Line 3,437:
===PowerShell Alternate Version===
===PowerShell Alternate Version===
A more "PowerShelly" way to do it.
A more "PowerShelly" way to do it.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Convert-Kelvin
function Convert-Kelvin
{
{
Line 3,465: Line 3,465:
}
}
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
21, 100 | Convert-Kelvin
21, 100 | Convert-Kelvin
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 3,571: Line 3,571:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>Procedure.d Kelvin2Celsius(tK.d) : ProcedureReturn tK-273.15 : EndProcedure
<syntaxhighlight 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 Kelvin2Fahrenheit(tK.d) : ProcedureReturn tK*1.8-459.67 : EndProcedure
Procedure.d Kelvin2Rankine(tK.d) : ProcedureReturn tK*1.8 : EndProcedure
Procedure.d Kelvin2Rankine(tK.d) : ProcedureReturn tK*1.8 : EndProcedure
Line 3,586: Line 3,586:
k$=Inkey() : Delay(50) : If RawKey()=#ESC : End : EndIf
k$=Inkey() : Delay(50) : If RawKey()=#ESC : End : EndIf
Until RawKey()
Until RawKey()
ForEver</lang>
ForEver</syntaxhighlight>
<pre>Temperatur Kelvin? 21
<pre>Temperatur Kelvin? 21
Conversion:
Conversion:
Line 3,595: Line 3,595:


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> while True:
<syntaxhighlight lang="python">>>> while True:
k = float(input('K ? '))
k = float(input('K ? '))
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
Line 3,605: Line 3,605:
K ? 222.2
K ? 222.2
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
K ? </lang>
K ? </syntaxhighlight>


===Python: Universal conversion===
===Python: Universal conversion===
This converts from any one of the units to all the others
This converts from any one of the units to all the others
<lang python>>>> toK = {'C': (lambda c: c + 273.15),
<syntaxhighlight lang="python">>>> toK = {'C': (lambda c: c + 273.15),
'F': (lambda f: (f + 459.67) / 1.8),
'F': (lambda f: (f + 459.67) / 1.8),
'R': (lambda r: r / 1.8),
'R': (lambda r: r / 1.8),
Line 3,628: Line 3,628:
<value> <K/R/F/C> ? 399.96 R
<value> <K/R/F/C> ? 399.96 R
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? </lang>
<value> <K/R/F/C> ? </syntaxhighlight>


=={{header|QB64}}==
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
<lang QB64>
Dim As Single Celsius, Kelvin, Fahrenheit, Rankine
Dim As Single Celsius, Kelvin, Fahrenheit, Rankine


Line 3,649: Line 3,649:
Kelvin = -300
Kelvin = -300
Wend
Wend
</syntaxhighlight>
</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 3,657: Line 3,657:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.


<lang Quackery> [ $ "bigrat.qky" loadfile ] now!
<syntaxhighlight lang="quackery"> [ $ "bigrat.qky" loadfile ] now!
[ 5 9 v* ] is r->k ( n/d --> n/d )
[ 5 9 v* ] is r->k ( n/d --> n/d )
Line 3,687: Line 3,687:
say " degrees Rankine" cr ] is task ( $ --> )
say " degrees Rankine" cr ] is task ( $ --> )
$ "21.00" task</lang>
$ "21.00" task</syntaxhighlight>


{{out}}
{{out}}
Line 3,698: Line 3,698:


=={{header|R}}==
=={{header|R}}==
<lang R>convert_Kelvin <- function(K){
<syntaxhighlight lang="r">convert_Kelvin <- function(K){
if (!is.numeric(K))
if (!is.numeric(K))
stop("\n Input has to be numeric")
stop("\n Input has to be numeric")
Line 3,712: Line 3,712:
convert_Kelvin(21)
convert_Kelvin(21)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,732: Line 3,732:
Although not exactly the shortest code,
Although not exactly the shortest code,
the converter function can turn any temperature into any other
the converter function can turn any temperature into any other
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (converter temp init final)
(define (converter temp init final)
(define to-k
(define to-k
Line 3,757: Line 3,757:
;Fahrenheit: -421.87
;Fahrenheit: -421.87
;Rankine: 37.800000000000004
;Rankine: 37.800000000000004
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 3,763: Line 3,763:


{{trans|Perl}}
{{trans|Perl}}
<lang perl6>my %scale =
<syntaxhighlight lang="raku" line>my %scale =
Celcius => { factor => 1 , offset => -273.15 },
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Rankine => { factor => 1.8, offset => 0 },
Line 3,774: Line 3,774:
for %scale.sort {
for %scale.sort {
printf "%12s: %7.2f\n", .key, $kelvin * .value<factor> + .value<offset>;
printf "%12s: %7.2f\n", .key, $kelvin * .value<factor> + .value<offset>;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,786: Line 3,786:
Alternative version that accepts the input in any of the four scales:
Alternative version that accepts the input in any of the four scales:


<lang perl6>while my $answer = prompt 'Temperature: ' {
<syntaxhighlight lang="raku" line>while my $answer = prompt 'Temperature: ' {
my $k = do given $answer {
my $k = do given $answer {
when s/:i C $// { $_ + 273.15 }
when s/:i C $// { $_ + 273.15 }
Line 3,798: Line 3,798:
say " { $k * 1.8 - 459.67 }℉";
say " { $k * 1.8 - 459.67 }℉";
say " { $k * 1.8 }R";
say " { $k * 1.8 }R";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Temperature: 0
<pre>Temperature: 0
Line 3,846: Line 3,846:
::* comments (annotation notes) allowed within the list
::* comments (annotation notes) allowed within the list
::* aligned output (whole numbers and decimal fractions)
::* aligned output (whole numbers and decimal fractions)
<lang rexx>/*REXX program converts temperatures for a number (8) of temperature scales. */
<syntaxhighlight lang="rexx">/*REXX program converts temperatures for a number (8) of temperature scales. */
numeric digits 120 /*be able to support some huge numbers.*/
numeric digits 120 /*be able to support some huge numbers.*/
parse arg tList /*get the specified temperature list. */
parse arg tList /*get the specified temperature list. */
Line 3,962: Line 3,962:
otherwise call serr 'illegal temperature scale:' y
otherwise call serr 'illegal temperature scale:' y
end /*select*/
end /*select*/
return</lang>
return</syntaxhighlight>
{{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>}}
{{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>
<pre>
Line 4,162: Line 4,162:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
k = 21.0 c = 0 r = 0 f = 0
k = 21.0 c = 0 r = 0 f = 0
convertTemp(k)
convertTemp(k)
Line 4,174: Line 4,174:
r = k * 1.8
r = k * 1.8
f = r - 459.67
f = r - 459.67
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>module TempConvert
<syntaxhighlight lang="ruby">module TempConvert


FROM_TEMP_SCALE_TO_K =
FROM_TEMP_SCALE_TO_K =
Line 4,223: Line 4,223:
end
end
end</lang>
end</syntaxhighlight>
Converts all eight scales to any other scale, by means of method_missing.
Converts all eight scales to any other scale, by means of method_missing.


Usage:
Usage:
<lang ruby>TempConvert.kelvin_to_celsius 100 #=> -173.15
<syntaxhighlight lang="ruby">TempConvert.kelvin_to_celsius 100 #=> -173.15
TempConvert.kelvin_to_fahrenheit 100 #=> -279.67
TempConvert.kelvin_to_fahrenheit 100 #=> -279.67
TempConvert.kelvin_to_rankine 100 #=> 180.0
TempConvert.kelvin_to_rankine 100 #=> 180.0
Line 4,237: Line 4,237:
TempConvert.newton_to_celsius 100 #=> 303.03
TempConvert.newton_to_celsius 100 #=> 303.03
TempConvert.newton_to_fahrenheit 100 #=> 577.45
TempConvert.newton_to_fahrenheit 100 #=> 577.45
# All 64 combinations possible</lang>
# All 64 combinations possible</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>[loop]
<syntaxhighlight lang="runbasic">[loop]
input "Kelvin Degrees";kelvin
input "Kelvin Degrees";kelvin
if kelvin <= 0 then end ' zero or less ends the program
if kelvin <= 0 then end ' zero or less ends the program
Line 4,247: Line 4,247:
rankine = kelvin * 1.8
rankine = kelvin * 1.8
print kelvin;" kelvin is equal to ";celcius; " degrees celcius and ";fahrenheit;" degrees fahrenheit and ";rankine; " degrees rankine"
print kelvin;" kelvin is equal to ";celcius; " degrees celcius and ";fahrenheit;" degrees fahrenheit and ";rankine; " degrees rankine"
goto [loop]</lang>
goto [loop]</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}
{{libheader|Scala}}
<lang Scala>object TemperatureConversion extends App {
<syntaxhighlight lang="scala">object TemperatureConversion extends App {


def kelvinToCelsius(k: Double) = k + 273.15
def kelvinToCelsius(k: Double) = k + 273.15
Line 4,277: Line 4,277:
}
}
} else println("Temperature not given.")
} else println("Temperature not given.")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,287: Line 4,287:


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>fn main() -> std::io::Result<()> {
<syntaxhighlight lang="rust">fn main() -> std::io::Result<()> {
print!("Enter temperature in Kelvin to convert: ");
print!("Enter temperature in Kelvin to convert: ");
let mut input = String::new();
let mut input = String::new();
Line 4,307: Line 4,307:


Ok(())
Ok(())
}</lang>
}</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==


<lang scheme>
<syntaxhighlight lang="scheme">
(import (scheme base)
(import (scheme base)
(scheme read)
(scheme read)
Line 4,331: Line 4,331:
(display "Fahrenheit: ") (display (kelvin->fahrenheit k)) (newline)
(display "Fahrenheit: ") (display (kelvin->fahrenheit k)) (newline)
(display "Rankine : ") (display (kelvin->rankine k)) (newline)))
(display "Rankine : ") (display (kelvin->rankine k)) (newline)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,342: Line 4,342:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "float.s7i";


Line 4,364: Line 4,364:
writeln("F: " <& fahrenheit(kelvin) digits 2 lpad 7);
writeln("F: " <& fahrenheit(kelvin) digits 2 lpad 7);
writeln("R: " <& rankine(kelvin) digits 2 lpad 7);
writeln("R: " <& rankine(kelvin) digits 2 lpad 7);
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 4,377: Line 4,377:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>var scale = Hash(
<syntaxhighlight lang="ruby">var scale = Hash(
Celcius => Hash.new(factor => 1 , offset => -273.15 ),
Celcius => Hash.new(factor => 1 , offset => -273.15 ),
Rankine => Hash.new(factor => 1.8, offset => 0 ),
Rankine => Hash.new(factor => 1.8, offset => 0 ),
Line 4,388: Line 4,388:
scale.keys.sort.each { |key|
scale.keys.sort.each { |key|
printf("%12s:%8.2f\n", key, kelvin*scale{key}{:factor} + scale{key}{:offset});
printf("%12s:%8.2f\n", key, kelvin*scale{key}{:factor} + scale{key}{:offset});
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,399: Line 4,399:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>
<syntaxhighlight lang="swift">
func KtoC(kelvin : Double)->Double{
func KtoC(kelvin : Double)->Double{
Line 4,423: Line 4,423:
var r=KtoR(kelvin : k)
var r=KtoR(kelvin : k)
print("\(r) Rankine")
print("\(r) Rankine")
</syntaxhighlight>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc temps {k} {
<syntaxhighlight lang="tcl">proc temps {k} {
set c [expr {$k - 273.15}]
set c [expr {$k - 273.15}]
set r [expr {$k / 5.0 * 9.0}]
set r [expr {$k / 5.0 * 9.0}]
set f [expr {$r - 459.67}]
set f [expr {$r - 459.67}]
list $k $c $f $r
list $k $c $f $r
}</lang>
}</syntaxhighlight>




Demonstrating:
Demonstrating:
<lang tcl>puts -nonewline "Enter a temperature in K: "
<syntaxhighlight lang="tcl">puts -nonewline "Enter a temperature in K: "
flush stdout
flush stdout
lassign [temps [gets stdin]] k c f r
lassign [temps [gets stdin]] k c f r
Line 4,441: Line 4,441:
puts [format "C: %.2f" $c]
puts [format "C: %.2f" $c]
puts [format "F: %.2f" $f]
puts [format "F: %.2f" $f]
puts [format "R: %.2f" $r]</lang>
puts [format "R: %.2f" $r]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,455: Line 4,455:
==={{header|Korn Shell}}===
==={{header|Korn Shell}}===
{{works with|ksh}}
{{works with|ksh}}
<lang bash>#!/bin/ksh
<syntaxhighlight lang="bash">#!/bin/ksh
# Temperature conversion
# Temperature conversion
typeset tt[1]=0.00 tt[2]=273.15 tt[3]=373.15
typeset tt[1]=0.00 tt[2]=273.15 tt[3]=373.15
Line 4,466: Line 4,466:
echo "Fahrenheit: $((t*18/10-459.67)) F"
echo "Fahrenheit: $((t*18/10-459.67)) F"
echo "Rankine: $((t*18/10)) R"
echo "Rankine: $((t*18/10)) R"
done</lang>
done</syntaxhighlight>


==={{header|bash}}===
==={{header|bash}}===
{{works with|Bourne Again SHell}}
{{works with|Bourne Again SHell}}
<lang bash>#!/bin/bash
<syntaxhighlight lang="bash">#!/bin/bash
# Temperature conversion
# Temperature conversion
tt[1]=0.00; tt[2]=273.15; tt[3]=373.15
tt[1]=0.00; tt[2]=273.15; tt[3]=373.15
Line 4,481: Line 4,481:
echo "Fahrenheit: $(bc<<<"scale=2;$t*18/10-459.67") F"
echo "Fahrenheit: $(bc<<<"scale=2;$t*18/10-459.67") F"
echo "Rankine: $(bc<<<"scale=2;$t*18/10") R"
echo "Rankine: $(bc<<<"scale=2;$t*18/10") R"
done</lang>
done</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
<lang ursa>decl double k
<syntaxhighlight lang="ursa">decl double k
while true
while true
out "Temp. in Kelvin? " console
out "Temp. in Kelvin? " console
Line 4,490: Line 4,490:
out "K\t" k endl "C\t" (- k 273.15) endl console
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
out "F\t" (- (* k 1.8) 459.67) endl "R\t" (* k 1.8) endl endl console
end while</lang>
end while</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==


<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
Option Explicit


Line 4,519: Line 4,519:
End Select
End Select
End Function
End Function
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Input in Kelvin : 21,00
<pre>Input in Kelvin : 21,00
Line 4,528: Line 4,528:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
WScript.StdOut.Write "Enter the temperature in Kelvin:"
WScript.StdOut.Write "Enter the temperature in Kelvin:"
tmp = WScript.StdIn.ReadLine
tmp = WScript.StdIn.ReadLine
Line 4,548: Line 4,548:
rankine = (k-273.15)*1.8+491.67
rankine = (k-273.15)*1.8+491.67
End Function
End Function
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 4,561: Line 4,561:


=={{header|Visual FoxPro}}==
=={{header|Visual FoxPro}}==
<lang vfp>#DEFINE ABSZC 273.16
<syntaxhighlight lang="vfp">#DEFINE ABSZC 273.16
#DEFINE ABSZF 459.67
#DEFINE ABSZF 459.67
LOCAL k As Double, c As Double, f As Double, r As Double, n As Integer, ;
LOCAL k As Double, c As Double, f As Double, r As Double, n As Integer, ;
Line 4,585: Line 4,585:
ENDDO
ENDDO
SET FIXED &cf
SET FIXED &cf
SET DECIMALS TO n</lang>
SET DECIMALS TO n</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,596: Line 4,596:
=={{header|Vlang}}==
=={{header|Vlang}}==
Note: round_sig in 0.3 or later
Note: round_sig in 0.3 or later
<lang vlang>import math
<syntaxhighlight lang="vlang">import math


fn main() {
fn main() {
Line 4,608: Line 4,608:
rankine := math.round_sig(kelvin * 1.8, 2)
rankine := math.round_sig(kelvin * 1.8, 2)
return celsius.str(), fahrenheit.str(), rankine.str()
return celsius.str(), fahrenheit.str(), rankine.str()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,619: Line 4,619:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var tempConv = Fn.new { |k|
var tempConv = Fn.new { |k|
Line 4,633: Line 4,633:


var ks = [0, 21, 100]
var ks = [0, 21, 100]
for (k in ks) tempConv.call(k)</lang>
for (k in ks) tempConv.call(k)</syntaxhighlight>


{{out}}
{{out}}
Line 4,654: Line 4,654:


=={{header|XLISP}}==
=={{header|XLISP}}==
<lang xlisp>(DEFUN CONVERT-TEMPERATURE ()
<syntaxhighlight lang="xlisp">(DEFUN CONVERT-TEMPERATURE ()
(SETQ *FLONUM-FORMAT* "%.2f")
(SETQ *FLONUM-FORMAT* "%.2f")
(DISPLAY "Enter a temperature in Kelvin.")
(DISPLAY "Enter a temperature in Kelvin.")
Line 4,666: Line 4,666:
(DISPLAY `(F = ,(- (* K 1.8) 459.67)))
(DISPLAY `(F = ,(- (* K 1.8) 459.67)))
(NEWLINE)
(NEWLINE)
(DISPLAY `(R = ,(* K 1.8))))</lang>
(DISPLAY `(R = ,(* K 1.8))))</syntaxhighlight>
{{out}}
{{out}}
<pre>(CONVERT-TEMPERATURE)
<pre>(CONVERT-TEMPERATURE)
Line 4,677: Line 4,677:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes;
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
real K, C, F, R;
real K, C, F, R;
[ChOut(0, ^K); K:= RlIn(0);
[ChOut(0, ^K); K:= RlIn(0);
Line 4,686: Line 4,686:
R:= F + 459.67;
R:= F + 459.67;
ChOut(0, ^R); RlOut(0, R); CrLf(0);
ChOut(0, ^R); RlOut(0, R); CrLf(0);
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,696: Line 4,696:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>K:=ask(0,"Kelvin: ").toFloat();
<syntaxhighlight lang="zkl">K:=ask(0,"Kelvin: ").toFloat();
println("K %.2f".fmt(K));
println("K %.2f".fmt(K));
println("F %.2f".fmt(K*1.8 - 459.67));
println("F %.2f".fmt(K*1.8 - 459.67));
println("C %.2f".fmt(K - 273.15));
println("C %.2f".fmt(K - 273.15));
println("R %.2f".fmt(K*1.8));</lang>
println("R %.2f".fmt(K*1.8));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,712: Line 4,712:
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==


<lang zxbasic>10 REM Translation of traditional basic version
<syntaxhighlight lang="zxbasic">10 REM Translation of traditional basic version
20 INPUT "Kelvin Degrees? ";k
20 INPUT "Kelvin Degrees? ";k
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
Line 4,722: Line 4,722:
90 PRINT f; " Degrees Fahrenheit"
90 PRINT f; " Degrees Fahrenheit"
100 PRINT r; " Degrees Rankine"
100 PRINT r; " Degrees Rankine"
110 GO TO 20</lang>
110 GO TO 20</syntaxhighlight>