30秒学会 JavaScript 片段 · 2022年12月23日

30秒学会 JavaScript 片段 – Convert between miles and kilometers in JavaScript

Miles to kilometers

In order to convert from miles to kilometers, you can use the conversion formula km = mi * 1.609344.

代码实现

const milesToKm = miles => miles * 1.609344;

milesToKm(5); // ~8.04672

Kilometers to miles

Conversely, the conversion formula from kilometers to miles is mi = km * 0.621371.

使用样例

const kmToMiles = km => km * 0.621371;

kmToMiles(8.1) // 5.0331051

翻译自:https://www.30secondsofcode.org/js/s/convert-miles-km