js 单位换算
时间: 2023-07-08 07:24:24 浏览: 76
JS中的单位换算可以使用一些内置的函数或者自己写函数实现,常见的单位换算有:
1. 温度单位换算:摄氏度与华氏度相互转换
```javascript
function celsiusToFahrenheit(celsius) {
let fahrenheit = (celsius * 1.8) + 32;
return fahrenheit;
}
function fahrenheitToCelsius(fahrenheit) {
let celsius = (fahrenheit - 32) / 1.8;
return celsius;
}
```
2. 长度单位换算:英寸与厘米相互转换
```javascript
function inchesToCentimeters(inches) {
let centimeters = inches * 2.54;
return centimeters;
}
function centimetersToInches(centimeters) {
let inches = centimeters / 2.54;
return inches;
}
```
3. 重量单位换算:磅与千克相互转换
```javascript
function poundsToKilograms(pounds) {
let kilograms = pounds * 0.45359237;
return kilograms;
}
function kilogramsToPounds(kilograms) {
let pounds = kilograms / 0.45359237;
return pounds;
}
```
这些函数只是一些常见的单位换算,根据需求可以自行编写其他单位的换算函数。