Temperature Converter

Code

The following functions convert celsius, fahrenheit and kelvin in to one another. These formulas have been coded in javascript, work to 6 decimal places and are on display below. You can easily port these to different languages.

Celsius to Fahrenheit

function c_f(c) {
	c = parseFloat(c.replace(/[^0-9\.\+-]/g, ""));
	var f = Math.round(((c * (9/5)) + 32) * 1000000) / 1000000;
	return (f === f) ? f : "";
}

Celsius to Kelvin

function c_k(c) {
	c = parseFloat(c.replace(/[^0-9\.\+-]/g, ""));
	var k = Math.round((c + 273.15) * 1000000) / 1000000;
	return (k === k) ? k : "";
}

Fahrenheit to Celsius

function f_c(f) {
	f = parseFloat(f.replace(/[^0-9\.\+-]/g, ""));
	var c = Math.round(((f - 32) * (5/9)) * 1000000) / 1000000;
	return (c === c) ? c : "";
}

Fahrenheit to Kelvin

function f_k(f) {
	f = parseFloat(f.replace(/[^0-9\.\+-]/g, ""));
	var k = Math.round(((f + 459.67) * (5/9)) * 1000000) / 1000000;
	return (k === k) ? k : "";
}

Kelvin to Celsius

function k_c(k) {
	k = parseFloat(k.replace(/[^0-9\.\+-]/g, ""));
	var c = Math.round((k - 273.15) * 1000000) / 1000000;
	return (c === c) ? c : "";
}

Kelvin to Fahrenheit

function k_f(k) {
	k = parseFloat(k.replace(/[^0-9\.\+-]/g, ""));
	var f = Math.round(((k * (9/5)) - 459.67) * 1000000) / 1000000;
	return (f === f) ? f : "";
}

Comments

Be the first to comment.


Anonymous

A preview of the comment you're writing will go here. Please wait a few seconds after typing.