473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert double to short ?

how to convert double to short ?
for example, I want to convert

double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;

shortVal = doubleVal1 - doubleVal2;

I want the result of shortVal = 3000 and store as short instead of double
value 3000.1
how can I do that?

Nov 13 '05
12 16551
"Alan" <al************ *@hotmail.com> wrote in message
news:bm******** ***@news.hgc.co m.hk...
how to convert double to short ?
for example, I want to convert

double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;

shortVal = doubleVal1 - doubleVal2;

I want the result of shortVal = 3000 and store as short instead of double value 3000.1
how can I do that?

You could try numeric_cast from boost library, it's simple for use,
http://www.boost.org/libs/conversion...m#numeric_cast

Robi
Nov 13 '05 #11
In <20************ *************** ***@flash-gordon.me.uk> Mark Gordon <sp******@fla sh-gordon.me.uk> writes:
On Sat, 18 Oct 2003 12:59:35 +0200
Sidney Cadot <si****@jigsaw. nl> wrote:
Mark Gordon wrote:
>>how to convert double to short ?
>>[snip]
> If you don't care about the direction of rounding you've just done
> it.


For C99 at least the rounding direction of implicit conversions and
casts from floating-point types to integer types is defined as
towards-zero, i.e. truncating (6.3.1.4 clause 1). I don't have a C89
standard handy, but I suspect the same is true there.


I thought than in C90 it was implementation defined.


Nope. C99 is no different from C89 on this particular issue.
Based on what I
have to hand I believe that float.h provides you with FLT_ROUNDS which
gives you information about the rounding used.


It doesn't apply here.

The rounding mode for floating-point addition is characterized by the
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
value of FLT_ROUNDS : -1 indeterminable, 0 toward zero, 1 to nearest, 2
toward positive infinity, 3 toward negative infinity. All other values
for FLT_ROUNDS characterize implementation-defined rounding behavior.

The text relevant to this discussion is:

3.2.1.3 Floating and integral

When a value of floating type is converted to integral type, the
fractional part is discarded. If the value of the integral part
cannot be represented by the integral type, the behavior is
undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
On Sat, 18 Oct 2003 15:46:54 +0800, "Alan"
<al************ *@hotmail.com> wrote:
how to convert double to short ?
With fear and trepidation. Plus range checking.
for example, I want to convert

double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;

shortVal = doubleVal1 - doubleVal2;

I want the result of shortVal = 3000 and store as short instead of double
value 3000.1
how can I do that?


--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3099
by: jose luis fernandez diaz | last post by:
Hi, The program below prints a unsigned short in binary base: #include <iostream> int main() { unsigned short mask =0x1; unsigned short us =0x0071;
12
2321
by: DanSteph | last post by:
hello all, I have a problem, I have a DOUBLE value like that 4065 4000 0000 0000 and I want to convert it to a DWORD like that: 4065 4000 if I do result=(DWORD)MyDouble it end with AA(170)
5
18831
by: kevinniu | last post by:
Hi everyone, In c#, what is the fastest way(include unsafe) to convert a array of bytes(which really contains the bytes of a double array) to a arry of double. thanks
4
47964
by: Stumped | last post by:
double DesignGlassPriceDbl =WindowAreaSqFtInt * DesignGlassPricePSF; int DesignGlassPriceInt= System.ConvertToInt32(DesignGlassPriceDbl); In the above code, if "DesignGlassPriceDbl" is 213.33, DesignGlassPriceInt evaluates to 104, not 213. How can I convert a double value to an Int? Dropping .33 in the process is OK, but not dropping (for some reason inexplicable to me) 109.33.
2
1648
by: ORC | last post by:
What is the difference between: Convert.ToDouble(Int16number); and: (double)Int16number; ? and which is the fastest? Thanks Ole
3
2259
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_CalculAXplusB(short int *IN_Tab_entree ,int IN_taille , double *OUT_Tab_sortie_freq , double *OUT_Tab_sortie, double IN_A,
3
7715
by: PeterK | last post by:
I am trying to set Public overridable CreditlimitS() as System.Data.SqlTypes.SqlMoney to Creditlimit as Double like CreditLimitS=creditlimit and get this error "Value of type double cannot be converted to System.Data.SqlTypes.SqlMoney " How do I get creditlimit into creditlimitS? There seems to be no conversion function. TIA
3
8749
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
22
11782
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.