开发者

Converting CURRENCY to a long

开发者 https://www.devze.com 2023-03-25 16:55 出处:网络
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 CURRENC开发者_Python百科Y type into a sprintf using %d

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 CURRENC开发者_Python百科Y type into a sprintf using %d

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


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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消