Temperature conversion: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 35:
=={{header|11l}}==
<
print(‘K ’k)
print(‘C ’(k - 273.15))
print(‘F ’(k * 1.8 - 459.67))
print(‘R ’(k * 1.8))</
{{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</
{{out}}
<pre>
Line 146:
=={{header|8th}}==
<
273.15 n:-
;
Line 178:
bye
;
</syntaxhighlight>
{{out}}
<pre>>8th temp.8th 21
Line 189:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
PROC K2C(REAL POINTER k,c)
Line 241:
ValR("373.15",k) Test("Water boils",k)
RETURN
</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Temperature_conversion.png Screenshot from Atari 8-bit computer]
Line 266:
=={{header|Ada}}==
<
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;</
{{out}}
Line 290:
=={{header|Aime}}==
<
show(integer symbol, real temperature)
{
Line 309:
return 0;
}</
{{out}}
<pre>aime$ aime -a tmp/tconvert 300
Line 318:
=={{header|ALGOL 68}}==
<
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</
{{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.
<
DECIMAL K, C, F, R;
WRITE( "Temperature in Kelvin:" );
Line 347:
WRITE( F, " degrees Fahrenheit" );
WRITE( R, " degrees Rankine" );
END</
=={{header|Amazing Hopper}}==
<syntaxhighlight 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>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
/* MISTRAL - a flavour of Hopper */
Line 393:
IMPRIMIR("RANKINE : ",temperatura, POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
#include <mistral.h>
Line 407:
"RANKINE : ", {temperatura} POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
<p>Or another (and last) version:</p>
<syntaxhighlight lang="amazing hopper">
#include <mistral.h>
Line 421:
"RANKINE : ", rankine, NL)
FINALIZAR
</syntaxhighlight>
{{out}}
<pre>
Line 440:
=={{header|APL}}==
Given a temperature in Kelvin, prints the equivalent in Kelvin, Celsius, Fahrenheit, and Rankine (in that order).
<
{{out}}
<
21 ¯252.15 ¯421.87 37.8</
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 )
<
-- KELVIN TO OTHER SCALE -----------------------------------------------------
Line 560:
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower</
{{Out}}
<pre>K 21.0
Line 571:
Or of course:
<
return ("K" & tab & (kelvinValue as real)) & ¬
(linefeed & "C" & tab & (kelvinValue - 273.15)) & ¬
Line 578:
end convertFromKelvin
convertFromKelvin(21)</
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:
<
set kelvinMeasurement to kelvinValue as degrees Kelvin
Line 595:
end convertFromKelvin
convertFromKelvin(21)</
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.
<
use framework "Foundation"
Line 632:
end convertFromKelvin
convertFromKelvin(21)</
=={{header|Arturo}}==
<
#[
celcius: k - 273.15
Line 644:
]
print convertKelvins 100</
{{out}}
Line 651:
=={{header|AutoHotkey}}==
<
. "Celsius:`t`t" kelvinToCelsius(21) " C`n"
. "Fahrenheit:`t" kelvinToFahrenheit(21) " F`n"
Line 667:
{
return, round(k * 1.8, 2)
}</
{{out}}
<pre>Kelvin: 21.00 K
Line 675:
=={{header|AutoIt}}==
<
Local Const $_KELVIN = 21
Line 694:
Return Round($degrees * 1.8, 2)
EndSelect
EndFunc ;==> Kelvin</
{{out}}
<pre>Kelvin: 21°
Line 703:
=={{header|AWK}}==
"Interactive" version, reading from stdin only:
<
BEGIN {
while (1) {
Line 721:
}
exit(0)
}</
"Regular" version, reading from input-file(s). <br>
Line 727:
{{works with|gawk}} BEGINFILE is a gawk-extension
<
BEGIN { print("# Temperature conversion\n") }
Line 746:
END { print("# Bye.") }
</syntaxhighlight>
{{out|Input}} the numeric value of the first word in each line is used as input for the conversion
Line 820:
=={{header|BASIC}}==
<
10 REM TRANSLATION OF AWK VERSION
20 INPUT "KELVIN DEGREES",K
Line 832:
100 PRINT R; " DEGREES RANKINE"
110 GOTO 20
</syntaxhighlight>
==={{header|QBasic}}===
<syntaxhighlight 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</
==={{header|True BASIC}}===
{{works with|QBasic}}
<
PRINT "Kelvin degrees (>=0): ";
INPUT K
Line 856:
PRINT "F = "; STR$(K * 1.8 - 459.67)
PRINT "R = "; STR$(K * 1.8)
END</
==={{header|Yabasic}}===
<
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</
==={{header|IS-BASIC}}===
<
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"</
==={{header|Sinclair ZX81 BASIC}}===
<
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"</
==={{Header|Tiny BASIC}}===
<
PRINT "Temperature in Kelvin?"
INPUT K
Line 893:
PRINT C," Celsius"
PRINT F," Fahrenheit"
PRINT R," Rankine"</
=={{header|BASIC256}}==
<
do
print "Kelvin degrees (>=0): ";
Line 906:
print "F = " + string(K * 1.8 - 459.67)
print "R = " + string(K * 1.8)
</syntaxhighlight>
=={{header|BBC BASIC}}==
<
REPEAT
INPUT "Kelvin degrees (>=0): " K
Line 919:
PRINT "R = " K * 1.8
END
</syntaxhighlight>
{{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.
<
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 </
{{out}}
Line 948:
=={{header|Bracmat}}==
<
= minus fixedpointnumber number decimals
. !arg:(#?number.~<0:~/#?decimals)
Line 993:
)
& done!
)</
{{out}}
<pre>Enter Kelvin temperature:21.00
Line 1,003:
=={{header|C}}==
<
#include <stdlib.h>
Line 1,031:
}
return 0;
}</
=={{header|C sharp|C#}}==
<
namespace TemperatureConversion
Line 1,064:
}
}
}</
<pre>
Line 1,075:
=={{header|C++}}==
<
#include <iostream>
#include <iomanip>
Line 1,130:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
{{out}}
<pre>
Line 1,149:
=={{header|Ceylon}}==
<
void printKelvinConversions(Float kelvin) {
Line 1,164:
printKelvinConversions(21.0);
}</
=={{header|Clojure}}==
{{trans|Common Lisp}}
<
(- 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.")))</
{{out}}
Line 1,188:
=={{header|CLU}}==
<
return(k)
end kelvin
Line 1,225:
stream$putl(po, f_form(c.func(k), 6, 2))
end
end start_up</
{{out}}
<pre>Enter temperature in Kelvin: 21
Line 1,235:
=={{header|COBOL}}==
{{works with|Visual COBOL}}
<
PROGRAM-ID. temp-conversion.
Line 1,270:
GOBACK
.</
{{out}}
Line 1,284:
Three functions define the necessary conversion formulas. A fancy format string is used to print these values.
<
(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>
{{out}}
Line 1,311:
=={{header|D}}==
<
return k - 273.15;
}
Line 1,343:
writefln("%2.2f K is below absolute zero", kelvin);
}
}</
{{out}}
<pre>
Line 1,356:
=={{header|Delphi}}==
<
program Temperature;
Line 1,398:
readln;
end.
</syntaxhighlight>
{{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"</
=={{header|Elena}}==
ELENA 4.1 :
<
convertKelvinToFahrenheit(x)
Line 1,449:
console.printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp));
console.readChar()
}</
{{out}}
<pre>
Line 1,460:
=={{header|Elixir}}==
<
def conversion(t) do
IO.puts "K : #{f(t)}"
Line 1,475:
end
Temperature.task</
{{out}}
Line 1,489:
=={{header|Erlang}}==
<
-module(temp_conv).
-export([main/0]).
Line 1,504:
f(A) ->
(round(A*100))/100 .
</syntaxhighlight>
{{out}}
<pre>
Line 1,519:
=={{header|Euphoria}}==
<syntaxhighlight 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>
{{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 </
{{Out}}
<pre>
Line 1,565:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(toUnit,
LAMBDA(n,
Line 1,580:
)
)
)</
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:
<
=LAMBDA(a,
LAMBDA(b,
Line 1,598:
)
)
)</
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">
# convert from Kelvin
நிரல்பாகம் கெல்வின்_இருந்து_மாற்று( k )
Line 1,746:
கெல்வின்_இருந்து_மாற்று( 273 ) #freezing pt of water
கெல்வின்_இருந்து_மாற்று( 30 + 273 ) #room temperature in Summer
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
// 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>
=={{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</
{{out}}
<pre>
Line 1,790:
=={{header|FOCAL}}==
<
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, !</
{{out}}
<pre>TEMPERATURE IN KELVIN:373.15
Line 1,804:
=={{header|Forth}}==
{{works with|GNU Forth}} for the command line handling
<
: k>°R ( F: kelvin -- rankine ) 1.8e0 f* ;
: °R>°F ( F: rankine -- fahrenheit ) 459.67e0 f- ;
Line 1,816:
then ;
main bye</
{{out}}
<pre>> gforthamd64 rosetta_temp_conv.fs 21
Line 1,826:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,851:
end subroutine
end program</
=={{header|FreeBASIC}}==
<
Sub convKelvin(temp As Double)
Line 1,873:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,889:
=={{header|Gambas}}==
<
Dim fKelvin As Float
Line 1,899:
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")
End</
Output:
<pre>
Line 1,909:
=={{header|Go}}==
<
import (
Line 1,935:
fmt.Printf("F %.2f\n", k*9/5-459.67)
fmt.Printf("R %.2f\n", k*9/5)
}</
{{out}}
<pre>
Line 1,946:
=={{header|Groovy}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 1,970:
=={{header|Haskell}}==
<
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</
Or with properly managed exceptions:
<
import System.Exit (die)
Line 2,012:
t <- liftIO getLine >>= tryRead "Could not read temp"
tryAssert "Temp cannot be negative" (t>=0)
return t</
=={{header|Icon}} and {{header|Unicon}}==
The following program works in both languages:
<
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</
Sample runs:
Line 2,042:
=={{header|J}}==
'''Solution''':<
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./ ]</
{{out|Example}}
<
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</
'''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}}==
<
public static void main(String args[]) {
if (args.length == 1) {
Line 2,109:
return k * 1.8;
}
}</
{{out}}
<pre>
Line 2,124:
===ES5===
<
var k2r = k => k * 1.8
var k2f = k => k2r(k) - 459.67
Line 2,137:
kCnv(21)
kCnv(295)</
{{out}}
<pre>
Line 2,148:
Deriving '''kelvinTranslations()''' from a more general '''heatBabel()''' function.
<
'use strict';
Line 2,186:
})();
</syntaxhighlight>
{{Out}}
Line 2,196:
=={{header|jq}}==
The hard part here is defining round/1 generically.<
# 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</
'''Example'''
<
21
Kelvin: 21
Line 2,243:
-1
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin</
=={{header|Julia}}==
<
"Celsius: $(round(k-273.15,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Rankine: $(round(k*1.8,2))")</
<pre>julia> cfr(21)
Kelvin: 21, Celsius: -252.15, Fahrenheit: -421.87, Rankine: 37.8</pre>
=={{header|Kotlin}}==
<
class Kelvin(val degrees: Double) {
Line 2,274:
println("F ${f.format(k.toFahreneit())}\n")
println("R ${f.format(k.toRankine())}")
}</
{{out}}
Line 2,290:
=={{header|Lambdatalk}}==
<
{def to-celsius {lambda {:k} {- :k 273.15}}}
-> to-celsius
Line 2,317:
-421.87 farenheit
37.8 rankine
</syntaxhighlight>
=={{header|Lasso}}==
<
local(
Line 2,370:
tempconverter(37.80, 'r')
'<br />'
tempconverter(69.80, 'f')</
<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}}==
<
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]"
}</
{{out}}
Line 2,400:
=={{header|LiveCode}}==
<
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</
Example<
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</
=={{header|Lua}}==
<
local c = k - 273.15
local r = k * 1.8
Line 2,430:
Rankine: %.2f °R
Fahrenheit: %.2f °F
]],convert_temp(21.0)))</
=={{header|Liberty BASIC}}==
<syntaxhighlight 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</
{{out}}
Line 2,453:
=={{header|Maple}}==
<
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);</
{{out}}
<pre>
Line 2,468:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
tempConvert[21]</
{{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}}
<
((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</
{{out}}
<pre>
Line 2,490:
=={{header|MiniScript}}==
<
print temp + " degrees in Kelvin is:"
Celsius = temp - 273.15
Line 2,501:
temp = input("Enter a temperature in Kelvin: ")
fromKelvin temp.val</
{{out}}
<pre>
Line 2,518:
=={{header|MiniZinc}}==
<
var float: celsius;
Line 2,529:
solve satisfy;
output ["K \(kelvin)\n", "C \(celsius)\n", "F \(fahrenheit)\n", "R \(rankine)\n"];</
{{out}}<pre>
Compiling temperature.mzn, additional arguments kelvin=1000;
Line 2,542:
=={{header|МК-61/52}}==
<
/ 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</
''Instruction:''
Line 2,572:
==={{header|mLite}}===
Temperature in Kelvin given on command line.
<
fun KtoF n = n * 1.8 - 459.67;
fun KtoR n = n * 1.8;
Line 2,588:
print "Rankine: "; println ` KtoR K
end
</syntaxhighlight>
=={{header|Nanoquery}}==
{{trans|Python}}
<
... 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 ? </
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols
Line 2,790:
return
</syntaxhighlight>
{{out}}
<pre pre style="height: 40ex; overflow: scroll">
Line 2,855:
=={{header|Never}}==
<syntaxhighlight 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>
{{output}}
<pre>
Line 2,882:
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(define (to-celsius k)
(- k 273.15)
Line 2,904:
)
)
</syntaxhighlight>
{{out}}
<pre>
Line 2,915:
=={{header|Nim}}==
{{libheader|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)</
Sample usage:
<pre>K ? 21.0
Line 2,928:
=={{header|Objeck}}==
<
class Temperature {
function : Main(args : String[]) ~ Nil {
Line 2,954:
}
}
</syntaxhighlight>
<pre>
Line 2,964:
=={{header|Objective-C}}==
<
int main(int argc, const char * argv[])
Line 2,982:
}
return 0;
}</
=={{header|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>
Sample session:
Line 3,020:
=={{header|Oforth}}==
<
: kelvinToFahrenheit 1.8 * 459.67 - ;
: kelvinToRankine 1.8 * ;
Line 3,027:
n kelvinToCelsius println
n kelvinToFahrenheit println
n kelvinToRankine println ;</
{{out}}
Line 3,038:
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
<
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.</
{{out}}
Line 3,106:
=={{header|Perl}}==
<
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};
}</
{{out}}
<pre>
Line 3,129:
=={{header|Phix}}==
Modified copy of [[Temperature_conversion#Euphoria|Euphoria]]
<!--<
<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>
<!--</
{{out}}
<pre>
Line 3,144:
=={{header|PHP}}==
<
while (true) {
Line 3,163:
}
}
}</
{{out}}
<pre>Enter a value in kelvin (q to quit): 21
Line 3,175:
=={{header|PicoLisp}}==
<
(de convertKelvin (Kelvin)
Line 3,186:
(tab (-3 8)
(car X)
(format ((cdr X) Kelvin) *Scl) ) ) )</
Test:
<syntaxhighlight lang
{{out}}
<pre>K 21.00
Line 3,196:
=={{header|PL/I}}==
<
/* PL/I **************************************************************
* 15.08.2013 Walter Pachl translated from NetRexx
Line 3,343:
End;
End;
End;</
{{out}}
<pre>
Line 3,359:
=={{header|Plain English}}==
<
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".</
{{out}}
<pre>
Line 3,403:
=={{header|PowerShell}}==
{{trans|Tcl}}
<
try{
$c = $k - 273.15
Line 3,423:
$input=Read-host "Enter a temperature in Kelvin"
temp $input</
{{Out}}
<pre>PS> ./TEMPS
Line 3,437:
===PowerShell Alternate Version===
A more "PowerShelly" way to do it.
<syntaxhighlight lang="powershell">
function Convert-Kelvin
{
Line 3,465:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
21, 100 | Convert-Kelvin
</syntaxhighlight>
{{Out}}
<pre>
Line 3,571:
=={{header|PureBasic}}==
<
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</
<pre>Temperatur Kelvin? 21
Conversion:
Line 3,595:
=={{header|Python}}==
<
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 ? </
===Python: Universal conversion===
This converts from any one of the units to all the others
<
'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> ? </
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
Dim As Single Celsius, Kelvin, Fahrenheit, Rankine
Line 3,649:
Kelvin = -300
Wend
</syntaxhighlight>
=={{header|Quackery}}==
Line 3,657:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
<
[ 5 9 v* ] is r->k ( n/d --> n/d )
Line 3,687:
say " degrees Rankine" cr ] is task ( $ --> )
$ "21.00" task</
{{out}}
Line 3,698:
=={{header|R}}==
<
if (!is.numeric(K))
stop("\n Input has to be numeric")
Line 3,712:
convert_Kelvin(21)
</syntaxhighlight>
{{out}}
Line 3,732:
Although not exactly the shortest code,
the converter function can turn any temperature into any other
<
(define (converter temp init final)
(define to-k
Line 3,757:
;Fahrenheit: -421.87
;Rankine: 37.800000000000004
</syntaxhighlight>
=={{header|Raku}}==
Line 3,763:
{{trans|Perl}}
<syntaxhighlight lang="raku"
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>;
}</
{{out}}
Line 3,786:
Alternative version that accepts the input in any of the four scales:
<syntaxhighlight lang="raku"
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";
}</
{{out}}
<pre>Temperature: 0
Line 3,846:
::* comments (annotation notes) allowed within the list
::* aligned output (whole numbers and decimal fractions)
<
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</
{{out|output|text= when using the input of: <tt> 98.6F to C, -40C, 0 c (water freezes), 37C (body temp), 100 C (water boils), 21 degrees Kelvin, 0 K (outer space?) </tt>}}
<pre>
Line 4,162:
=={{header|Ring}}==
<
k = 21.0 c = 0 r = 0 f = 0
convertTemp(k)
Line 4,174:
r = k * 1.8
f = r - 459.67
</syntaxhighlight>
=={{header|Ruby}}==
<
FROM_TEMP_SCALE_TO_K =
Line 4,223:
end
end</
Converts all eight scales to any other scale, by means of method_missing.
Usage:
<
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</
=={{header|Run BASIC}}==
<
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]</
=={{header|Scala}}==
{{libheader|Scala}}
<
def kelvinToCelsius(k: Double) = k + 273.15
Line 4,277:
}
} else println("Temperature not given.")
}</
{{out}}
<pre>
Line 4,287:
=={{header|Rust}}==
<
print!("Enter temperature in Kelvin to convert: ");
let mut input = String::new();
Line 4,307:
Ok(())
}</
=={{header|Scheme}}==
<
(import (scheme base)
(scheme read)
Line 4,331:
(display "Fahrenheit: ") (display (kelvin->fahrenheit k)) (newline)
(display "Rankine : ") (display (kelvin->rankine k)) (newline)))
</syntaxhighlight>
{{out}}
Line 4,342:
=={{header|Seed7}}==
<
include "float.s7i";
Line 4,364:
writeln("F: " <& fahrenheit(kelvin) digits 2 lpad 7);
writeln("R: " <& rankine(kelvin) digits 2 lpad 7);
end func;</
{{out}}
Line 4,377:
=={{header|Sidef}}==
{{trans|Perl}}
<
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});
}</
{{out}}
<pre>
Line 4,399:
=={{header|Swift}}==
<
func KtoC(kelvin : Double)->Double{
Line 4,423:
var r=KtoR(kelvin : k)
print("\(r) Rankine")
</syntaxhighlight>
=={{header|Tcl}}==
<
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
}</
Demonstrating:
<
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]</
{{out}}
<pre>
Line 4,455:
==={{header|Korn Shell}}===
{{works with|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</
==={{header|bash}}===
{{works with|Bourne Again SHell}}
<
# 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</
=={{header|Ursa}}==
<
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</
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 4,519:
End Select
End Function
</syntaxhighlight>
{{out}}
<pre>Input in Kelvin : 21,00
Line 4,528:
=={{header|VBScript}}==
<syntaxhighlight 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>
{{Out}}
Line 4,561:
=={{header|Visual FoxPro}}==
<
#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</
{{out}}
<pre>
Line 4,596:
=={{header|Vlang}}==
Note: round_sig in 0.3 or later
<
fn main() {
Line 4,608:
rankine := math.round_sig(kelvin * 1.8, 2)
return celsius.str(), fahrenheit.str(), rankine.str()
}</
{{out}}
Line 4,619:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var tempConv = Fn.new { |k|
Line 4,633:
var ks = [0, 21, 100]
for (k in ks) tempConv.call(k)</
{{out}}
Line 4,654:
=={{header|XLISP}}==
<
(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))))</
{{out}}
<pre>(CONVERT-TEMPERATURE)
Line 4,677:
=={{header|XPL0}}==
<
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);
]</
{{out}}
<pre>
Line 4,696:
=={{header|zkl}}==
<
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));</
{{out}}
<pre>
Line 4,712:
=={{header|ZX Spectrum Basic}}==
<
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</
|