正确设置石头和磅的格式? [英] Format Stones and Pounds correctly?

查看:170
本文介绍了正确设置石头和磅的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图表,以磅和磅(lbs)为单位显示重量.

I have a chart used to display weight in Stones and Pounds (lbs).

图表由记录中的数据填充,权重数据类型为Double.

The chart is populated by data from a record, for weight the data type is Double.

记录数据是在运行时进行编辑的,我需要知道一种正确格式化输入数据的方法.

The record data is edited at runtime and I need to know a way of formatting the entered data correctly.

为了更好地理解,首先查看这些样本值,它们表示为Stones和lbs:

To better understand, first look at these sample values, they are represented as Stones and lbs:

  • 8.09(8块石头和9磅)
  • 12.03(12块石头和3磅)
  • 14.16(14块石头和16磅)
  • 11.13(11块石头和13磅)
  • 17.14(17块石头和14磅)

一块石头只有14磅,因此输入的任何值都超过.13时,应该将石头的值增加1,然后从.00开始降低磅的值-因此请记住,在上面的示例2中这些值中的不正确:

There are only 14 pounds in a stone, so any value entered that is over .13 should increase the stones value by 1 and lower the pounds value starting back from .00 - so with that in mind, from the above sample two of those values are incorrect:

  • 14.16
  • 17.14

它们应该是:

  • 15.02
  • 18.00

是否有一个内置的数学函数可以正确格式化/舍入石头和磅?

Is there a built-in math function that can correctly format/round stones and pounds correctly?

如果不是这样,我将非常有兴趣看到一个答案,该答案显示了解决此问题的逻辑或方法.

If not I would be really interested to see an answer that shows the logic or method of approach to solving this.

我考虑过检查磅数部分,如果值> 0.13,那么我会增加结石,但是我不确定如何最好地做到这一点,如果值可能是13.76,那么我不知道该怎么做将石头和磅重改变成(这就是我开始迷惑自己的地方).

I thought of checking the pounds part and if the value > 0.13 then I increment the stones, but then I am not sure how best to do this especially, if the value could be something like 13.76 then I wouldn't know what to change the stones and pounds into (this is where I start to confuse myself).

谢谢.

推荐答案

对于磅,您用Frac(weight)得到小数部分,然后乘以100.然后使用Round以整数形式得到它:

For pounds you get the fractional part with Frac(weight) and then multiply by 100. Then use Round to get it in integer form:

pounds := Round(100*Frac(weight));

对于石头来说,只是Trunc:

stones := Trunc(weight);

从另一个方向上做

weight := stones + pounds/100.0;

使用这些功能,您可以轻松完成其余的工作.磅值大于14的有效性检查很容易处理.例如:

With these functions you can readily do the rest. The validity checks for values of pounds greater than 14 are easy to deal with. For example:

stones := Trunc(weight);
pounds := Round(100*Frac(weight));
stones := stones + pounds div 14;
pounds := pounds mod 14;

如果您在通用库的任何位置找到此代码,我会感到非常惊讶.那是因为这是一种非常差的重量存储方式.如果要使用浮点格式,则应该这样:

I'd be very surprised if you found this code anywhere in a general purpose library. That's because it's a very poor way to store weight. If you are going to use a floating point format you should do it like this:

weight := stones + pounds/14.0;

从另一个方向看,您会这样做:

In the other direction you would do it like this:

stones := Trunc(weight);
pounds := Round(14*Frac(weight));
stones := stones + pounds div 14;
pounds := pounds mod 14;

不幸的是,您仍然需要进行div/mod随机播放.想象一下,例如weight=9.99时会发生什么.

Unfortunately you still need to do the div/mod shuffle. Imagine what happens when weight=9.99, for example.

以这种方式进行操作使对浮点值的算术更加明智.例如,假设您测量了10个人的体重,并且想知道总数.用真正的浮点表示法来做到这一点是有意义的,但是对于您的表示法则没有意义.

Doing it this way makes arithmetic on the floating point values more sensible. For example, suppose you have measured weights of 10 people and want to know the total. It makes sense to do that with true floating point representation, but not with your representation.

要看到这一点,假设这10个人的重量均为零石头(10磅).我认识的人很小.但是您可以将其称为0.1.加起来有10批,您的权重为1.0.但很明显,实际价值是100磅,或7石头2磅.

To see that, suppose these 10 people, and they all weigh zero stones, 10 pounds. Very small people I know. But you would call that 0.1. Add up 10 lots of that and you have a weight of 1.0. But it's clear that the actual value is 100 pounds, or 7 stone two pounds.

但是,如果您减去10磅并将其喂入:

But if you take 10 pounds and feed it into:

weight := stones + pounds/14.0;

那么您发现权重值为10/14.加10手就能得到100/14,好吧,我敢肯定,你一定会喜欢我的!

then you find a weight value of 10/14. Add 10 lots of that to get 100/14, and well, I'm sure you get my drift!

另一种明显的存储数据的方式是磅.整数或浮点数都可以.

The other obvious way to store such data is as pounds. Either integers or floating point could make sense.

这篇关于正确设置石头和磅的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆