即时测量转换

发布于 2024-07-25 07:54:42 字数 242 浏览 10 评论 0原文

我想问一下:动态测量转换,详细信息如下:

要求:要显示单位测量并考虑设置。 担忧: - 只有基本(中性)单位计量将被存储在数据库中,并且是一次性决定的。

  • 网格控件直接绑定到我们的业务对象,因此转换值很复杂。

问题:

如何显示不同的单位度量(遵循设置),考虑控件绑定到业务对象?

我们将不胜感激您的帮助。 谢谢伊卡德维

I'd like to ask re: measurement conversion on the fly, here's the detail :

Requirement: To display unit measurement with consider setting.
Concerns:
- Only basic (neutral) unit measurement is going to be stored in database, and it is decided one time.

  • The grid control has direct binding to our business object therefore it has complexity to do conversion value.

Problem:

How to display different unit measurement (follow a setting), consider that controls are bind to business object?

Your kind assistance will be appreciated. Thank you

ikadewi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

纸短情长 2024-08-01 07:54:43

如果我正确理解了你的问题(你有点含糊......),你想以一种方式存储测量数据,但让用户可以选择以不同的方式显示它(使用不同的单位)。 您没有指定您正在使用哪种技术/语言环境,但(至少)有一种非常简单的方法可以做到这一点:创建一个转换器类。

下面是一些伪 C# 框架代码,如果您的测量数据是长度,以毫米为单位存储。 您可能可以弄清楚如何对您正在测量的任何内容以及您想要显示的内容使用相同的方法:

class LenghtConverter {

    double ToCentimeters(double millimeters) {
        // 1 centimeter = 10 millimeters
        return millimeters / 10;
    }

    double ToInches(double millimeters) {
        // 1 inch = 25.4 millimeters
        return millimeters / 25.4
    }

    // You get the drift. Add whatever conversions you need. If you wish, 
    // you can return strings instead of numbers, and append the unit
    // signature as well.
}

现在,您可以在网格中使用某种表示语法来显示数据。 我正在编造一些东西来给你一个想法,因为我喜欢 ASP.NET,所以语法与此非常相似。 希望您能原谅我=)

您的输出不仅仅是

<%= MyMeasurement.Data %>

以存储的方式显示测量数据,

<%= LenghtConverter.ToInches(MyMeasurement.Data) %>

而是以英寸为单位显示结果。

如果您实际上使用的是 C#(或者我想是 VB.NET),.NET 3.5 中有一个很好的功能,称为扩展方法,您可能想使用它。 这会让你用更酷、更精简的语法输出

<%= MyMeasurement.Data.ToInches() %>

If I've understood your question correctly (you are a little vague...) you want to store measurement data in one way, but give the users the option to display it in different ways (using different units). You don't specify which technology/language environment you're using, but there is (at least) one pretty straightforward way to do this: create a converter class.

Here's some pseudo-C# skeleton code if your measurement data is lengths, stored in millimeters. You can probably figure out how to use the same approach for whatever you're measuring, and however you want to display it:

class LenghtConverter {

    double ToCentimeters(double millimeters) {
        // 1 centimeter = 10 millimeters
        return millimeters / 10;
    }

    double ToInches(double millimeters) {
        // 1 inch = 25.4 millimeters
        return millimeters / 25.4
    }

    // You get the drift. Add whatever conversions you need. If you wish, 
    // you can return strings instead of numbers, and append the unit
    // signature as well.
}

Now, in your grid you display your data with some kind of presentation syntax. I'm making something up to give you an idea, and since I'm into ASP.NET the syntax is pretty similar to that. Hope you'll excuse me for that =)

Instead of just

<%= MyMeasurement.Data %>

to display the measurement data in the way it was stored, you output with

<%= LenghtConverter.ToInches(MyMeasurement.Data) %>

which will display the result in inches.

If you're actually using C# (or VB.NET, I suppose) there is a nice feature available in .NET 3.5 called Extension Methods that you might want to use instead. That would let you output with the somewhat cooler and more streamlined syntax

<%= MyMeasurement.Data.ToInches() %>
与风相奔跑 2024-08-01 07:54:43

我有一个 QML Qt C++ UI。 它与后端应用程序交互。
UI 支持英制和英制 公制模式。 用户可以在运行时从 UI 中进行此选择。
用户可以通过 UI 查看和编辑数据值。
后端应用程序仅在英制模式下工作。

C++ 实用程序对象作为上下文属性公开给 QML。 该实用程序对象具有以下方法:

  • 设置和获取测量系统。
  • 将单位字符串从英制转换为公制。 示例:°F 至°C。
  • 将数据值从英制转换为公制,将公制转换为英制。 示例:华氏温度到摄氏温度 -> 50 到 10,摄氏度到华氏度 -> 0 到 32。C

++ 数据对象具有以下 2 个属性:

Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString unitString READ unitString NOTIFY unitStringChanged)
// value - In Imperial mode, you get Imperial value. In Metric mode, you get Metric value.
// unitString - In Imperial mode, you get Imperial units. In Metric mode, you get Metric units.

QVariant data::value()
{
    // fetch Imperial data value from back-end application
    // get current System of measurement
    // if current System of measurement is Metric, convert data value from Imperial to Metric
    // return data value
}

QString data::unitString()
{
    // fetch Imperial unit from back-end application
    // get current System of measurement
    // if current System of measurement is Metric, convert unit from Imperial to Metric
    // return unit
}

void data::setValue(QVariant value)
{
    // get current System of measurement
    // if current System of measurement is Metric, convert value from Metric to Imperial
    // write value to back-end Controller application
}

I have a QML Qt C++ UI. It interfaces with a back-end application.
UI supports both Imperial & Metric modes. User can make this selection from UI at runtime.
User can view and edit data values via the UI.
Back-end application works only in Imperial mode.

A C++ utility object is exposed to QML as a context property. This utility object has methods to:

  • Set and Get the System of measurement.
  • Convert unit string from Imperial to Metric. Example: °F to °C.
  • Convert data value from Imperial to Metric and Metric to Imperial. Example: Fahrenheit to Celsius -> 50 to 10, Celsius to Fahrenheit -> 0 to 32.

C++ data object has these 2 properties:

Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString unitString READ unitString NOTIFY unitStringChanged)
// value - In Imperial mode, you get Imperial value. In Metric mode, you get Metric value.
// unitString - In Imperial mode, you get Imperial units. In Metric mode, you get Metric units.

QVariant data::value()
{
    // fetch Imperial data value from back-end application
    // get current System of measurement
    // if current System of measurement is Metric, convert data value from Imperial to Metric
    // return data value
}

QString data::unitString()
{
    // fetch Imperial unit from back-end application
    // get current System of measurement
    // if current System of measurement is Metric, convert unit from Imperial to Metric
    // return unit
}

void data::setValue(QVariant value)
{
    // get current System of measurement
    // if current System of measurement is Metric, convert value from Metric to Imperial
    // write value to back-end Controller application
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文