ColdFusion - 将重量(磅)转换为磅和盎司

发布于 2024-11-30 14:32:39 字数 452 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

別甾虛僞 2024-12-07 14:32:39

像这样的东西(未经广泛测试)。

编辑:如果输入可以为负数,则使用 abs() 值进行计算

<cfset theInput  = 0.25>
<!--- round down to get total pounds --->
<cfset lbs       = int(theInput)>
<!--- extract remainder. multiply by 16 and round up --->
<cfset ounces    = ceiling((theInput - lbs)  * 16)>
<cfoutput>#lbs# pounds #ounces# ounces</cfoutput>

Something like this (not extensively tested).

EDIT: If the input can be negative, use the abs() value for calculations

<cfset theInput  = 0.25>
<!--- round down to get total pounds --->
<cfset lbs       = int(theInput)>
<!--- extract remainder. multiply by 16 and round up --->
<cfset ounces    = ceiling((theInput - lbs)  * 16)>
<cfoutput>#lbs# pounds #ounces# ounces</cfoutput>
桃酥萝莉 2024-12-07 14:32:39

整数除法和模数应该为您提供所需的值。

<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
weightInPounds = MyOunces \ 16;
// REMAINDER IS OUNCES - ROUND UP 
remainderOunces = ceiling(MyOunces MOD 16);
</cfscript>

Integer division and Modulus should give you the values you need.

<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
weightInPounds = MyOunces \ 16;
// REMAINDER IS OUNCES - ROUND UP 
remainderOunces = ceiling(MyOunces MOD 16);
</cfscript>
空城旧梦 2024-12-07 14:32:39

这应该可以做到:

<cffunction name="PoundConverter" returntype="string">
    <cfargument name="Pounds" type="numeric" required="true" hint="" />
    <cfset var TotalPounds = Fix(Arguments.Pounds) />
    <cfset var TotalOunces = Ceiling((Arguments.Pounds - TotalPounds) * 16) />
    <cfreturn TotalPounds & " pounds and " & TotalOunces & " ounces" />
</cffunction>


<cfoutput>
    #PoundConverter(3.1565)#<br />
    #PoundConverter(1.512)#
</cfoutput>

This should do it:

<cffunction name="PoundConverter" returntype="string">
    <cfargument name="Pounds" type="numeric" required="true" hint="" />
    <cfset var TotalPounds = Fix(Arguments.Pounds) />
    <cfset var TotalOunces = Ceiling((Arguments.Pounds - TotalPounds) * 16) />
    <cfreturn TotalPounds & " pounds and " & TotalOunces & " ounces" />
</cffunction>


<cfoutput>
    #PoundConverter(3.1565)#<br />
    #PoundConverter(1.512)#
</cfoutput>
甜味超标? 2024-12-07 14:32:39

你几乎已经拥有了你需要的东西。要提取磅数,请除以 16。余数(“mod”)就是盎司。

<cfscript>
    function poundsandounces( initvalue ) {
        var rawvalue = val( initvalue ) * 16;
        var lbs = int( rawvalue / 16 );
        var oz = ceiling( rawvalue % 16 );
        return "#lbs# pounds #oz# ounces";
    }
</cfscript>

<cfoutput>#poundsandounces( 0.25 )#</cfoutput>

You pretty much have what you need. To extract the number of pounds, divide by 16. The remainder (the "mod") is the ounces.

<cfscript>
    function poundsandounces( initvalue ) {
        var rawvalue = val( initvalue ) * 16;
        var lbs = int( rawvalue / 16 );
        var oz = ceiling( rawvalue % 16 );
        return "#lbs# pounds #oz# ounces";
    }
</cfscript>

<cfoutput>#poundsandounces( 0.25 )#</cfoutput>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文