ColdFusion - 将重量(磅)转换为磅和盎司
我正在 ColdFusion CFSCRIPT 中编写一个小转换片段。
我需要将以磅为单位的重量转换为磅和盎司。
因此,3.1565 需要变成 3 磅 3 盎司。 1.512 将变为 1 磅 9 盎司(盎司向上舍入)。
0.25 将变为 0 磅 4 盎司。
我的想法是以磅为单位的总重量乘以十六,就得到了总盎司。然后我需要除以十六来提取偶数磅,剩下的就是盎司。我真的不知道如何使用高效的代码准确地做到这一点。
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
// REMAINDER IS OUNCES - ROUND UP
}
</cfscript>
I am writing a little conversion piece in ColdFusion CFSCRIPT.
I need to convert a weight in pounds to pounds and ounces.
So, 3.1565 needs to become 3 pounds and 3 ounces. 1.512 will become 1 pound and 9 ounces (round up the ounces).
0.25 will become 0 pounds and 4 ounces.
My thought is to take the total weight in pounds and multiply it by sixteen, which will give me the total ounces. Then I'll need to extract the even pounds by dividing by sixteen and the remainder will be the ounces. I really don't know how to do this accurately and with efficient code.
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
// REMAINDER IS OUNCES - ROUND UP
}
</cfscript>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西(未经广泛测试)。
编辑:如果输入可以为负数,则使用
abs()
值进行计算Something like this (not extensively tested).
EDIT: If the input can be negative, use the
abs()
value for calculations整数除法和模数应该为您提供所需的值。
Integer division and Modulus should give you the values you need.
这应该可以做到:
This should do it:
你几乎已经拥有了你需要的东西。要提取磅数,请除以 16。余数(“mod”)就是盎司。
You pretty much have what you need. To extract the number of pounds, divide by 16. The remainder (the "mod") is the ounces.