将 CURRENCY 转换为 long

发布于 2024-11-27 18:42:03 字数 115 浏览 1 评论 0 原文

如何将 CURRENCY 类型转换为 long 类型?

我需要能够做到这一点,因为我想使用 %d 将 CURRENCY 类型的值放入 sprintf

我在这方面遇到了困难,感谢帮助:)

How can I convert CURRENCY type to a long type?

I need to be able to do this because I want to put the value of the CURRENCY type into a sprintf using %d

I'm having a hard time with this one, help is appreciated :)

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

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

发布评论

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

评论(3

枕花眠 2024-12-04 18:42:03

I am assuming you want to cast a CURRENCY value to a long. As you can see in the documentation, a currency is simply a 64 bit integer, storing 1/10000ths of a currency unit. With unit I mean Dollar, British Pound, Euro, etc. Not pennies, cents and the like. All you have to do is to do this: long value = (long)currency.int64. Remember that if the value in the member int64 is larger than LONG_MAX or smaller than LONG_MIN, then you will get truncation errors.

Why do you need to convert the value into a long? Can't you use the CURRENCY union as is?

混吃等死 2024-12-04 18:42:03

如果货币是字符串,您可以使用

sscanf(currency, "%d", &myLong);

如果它是任何其他基本数据类型,您可以直接进行类型转换,例如

long myLong = (long)currency;

如果您有有关数据类型的更多信息,请将其编辑到您的帖子中。

If the currency is a string, you can use

sscanf(currency, "%d", &myLong);

If it is any other basic data type, you can just typecast such as

long myLong = (long)currency;

If you have more information on the data type, please edit it into your post.

差↓一点笑了 2024-12-04 18:42:03

CURRENCY 是一个在代码中某处定义的类(即它不是本机 C++ 类型)。

如果您的类是由其他人编写的,则您的类可能已经具有转换为 int (或 long)的能力。您可以尝试通过以下方式解决这个问题:(1)读取头文件或(2)编译它

    CURRENCY mymoney;
    printf("%d",static_cast<long>(mymoney));

,看看编译器是否允许您摆脱这一点 - 如果是这样,那么很可能已经为该定义了一个强制转换运算符班级。

许多人不喜欢您在 C++ 代码中使用 sprintf,但更喜欢您使用流。如果是这样,则可能定义了 << 运算符 - 您可以尝试通过 (1) 读取包含文件或 (2) 编译此代码来确定这一点。

    CURRENCY mymoney;
    std::cout << mymoney << std:: endl;

如果没有,您可能必须编写代码以重载Currency 类上的< 和/或长强制转换运算符,以返回流输出和强制转换为其他类型所需的值。

CURRENCY is a class which is defined somewhere in your code (i.e. it is not a native C++ type).

Your class may already have the capability to cast to int (or long) if it is written by someone else. You can try to figure that out by (1) readinh the header files or (2) compileing this

    CURRENCY mymoney;
    printf("%d",static_cast<long>(mymoney));

and see if the compiler let you get away with that -- and if it does then most likely there is already a cast operator defined for the class.

Many people don't like you to use sprintf in C++ code, but prefer you to use streams. If so there may be a << operator defined instead -- you can try to determine that by (1) reading the include files, or (2) compiling this code

    CURRENCY mymoney;
    std::cout << mymoney << std:: endl;

If not you may have to write the code to overload the <<operator and/or the long cast operator on the Currency class to return the values you need for stream output and casting to other types.

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