简体   繁体   English

使用 toFixed 问题在 javascript 中将米转换为英尺,反之亦然

[英]Converting metres to feet and vice versa in javascript using toFixed issue

I'm trying to produce a unit conversion from metres to feet and vice versa.我正在尝试生成从米到英尺的单位转换,反之亦然。 I've seen this question asked many times, but what I haven't found is an an answer to why using the.toFixed(1) does not calculate to 1 decimal place in all cases.我已经看到这个问题被问了很多次,但我没有找到一个答案,为什么使用 the.toFixed(1) 在所有情况下都不能计算到小数点后一位。 I know JavaScript is finicky with such things, and I'm still trying to understand it.我知道 JavaScript 对这些东西很挑剔,我仍在努力理解它。 I've probably gone down the wrong route, so any advice is welcome.我可能走错了路,所以欢迎任何建议。

It's fine for converting 1 metre to feet, but converting 1 foot to metres, the result is to 17 decimal places.可以将 1 米转换为英尺,但将 1 英尺转换为米,结果是小数点后 17 位。

If you try converting 1.1 metres to feet that's fine but vice versa you get 17 decimal places.如果您尝试将 1.1 米转换为英尺,那很好,反之亦然,您会得到 17 位小数。

It's gets even worse when you start to convert 1.2 metres to feet.当您开始将 1.2 米转换为英尺时,情况会变得更糟。

I've produced a simple snippet to illustrate my issue.我制作了一个简单的片段来说明我的问题。 If there's anyone who can help with my solution so that the result is only ever 1 decimal place, I'd be grateful.如果有人可以帮助我的解决方案,以使结果只有小数点后 1 位,我将不胜感激。

 // Converting Metres to Feet and Feet to Metres const conversion = 3.281 console.log('--- Conversion of 1 Metre / 1 Foot ---') let unitFoot1 = 1 let unitMetre1 = 1 console.log('1 Metre is equal to', unitFoot1 * conversion.toFixed(1), 'Feet') console.log('1 Foot is equal to', unitMetre1 / conversion.toFixed(1), 'Metres') console.log('--- Conversion of 1.1 Metres / 1.1 Feet ---') let unitFoot1_1 = 1.1 let unitMetre1_1= 1.1 console.log('1.1 Metre is equal to', unitFoot1_1 * conversion.toFixed(1), 'Feet') console.log('1.1 Foot is equal to', unitMetre1_1 / conversion.toFixed(1), 'Metres') console.log('--- Conversion of 1.2 Metres / 1.2 Feet ---') let unitFoot1_2 = 1.2 let unitMetre1_2= 1.2 console.log('1.2 Metres is equal to', unitFoot1_2 * conversion.toFixed(1), 'Feet') console.log('1.2 Foot is equal to', unitMetre1_2 / conversion.toFixed(1), 'Metres')

You are calling toFixed on conversion , but you want to call it at the result of your calculations: (unitFoot1 * conversion).toFixed(1) .您正在调用toFixed on conversion ,但您想在计算结果时调用它: (unitFoot1 * conversion).toFixed(1)

 // Converting Metres to Feet and Feet to Metres const conversion = 3.281 console.log('--- Conversion of 1 Metre / 1 Foot ---') let unitFoot1 = 1 let unitMetre1 = 1 console.log('1 Metre is equal to', (unitFoot1 * conversion).toFixed(1), 'Feet') console.log('1 Foot is equal to', (unitMetre1 / conversion).toFixed(1), 'Metres') console.log('--- Conversion of 1.1 Metres / 1.1 Feet ---') let unitFoot1_1 = 1.1 let unitMetre1_1= 1.1 console.log('1 Metre is equal to', (unitFoot1_1 * conversion).toFixed(1), 'Feet') console.log('1 Foot is equal to', (unitMetre1_1 / conversion).toFixed(1), 'Metres') console.log('--- Conversion of 1.2 Metres / 1.2 Feet ---') let unitFoot1_2 = 1.2 let unitMetre1_2= 1.2 console.log('1 Metre is equal to', (unitFoot1_2 * conversion).toFixed(1), 'Feet') console.log('1 Foot is equal to', (unitMetre1_2 / conversion).toFixed(1), 'Metres')

I know JavaScript is finicky with such things我知道 JavaScript 对这些东西很挑剔

That is not related to JavaScript but happens in any language if you calculate with floating-point numbers.这与 JavaScript 无关,但如果您使用浮点数进行计算,则会在任何语言中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM