Did that, returns a 0.noobie said:yea, i think i need some sleep
just cast it..
int a=(int) myDouble;
noobie said:then its already 0 or 0.xx
thats the simplest way of doing it.
Sep102 said:Code:hitsmax = (int)( ((1 / speed) * 3000) + 0.5 );
Casting it to int drops the fractional part of it completely, adding 0.5 to it before doing so rounds properly (i.e. 3.5 rounds to 4, 3.49 rounds to 3).
Sep102 said:Casting it to int drops the fractional part of it completely, adding 0.5 to it before doing so rounds properly (i.e. 3.5 rounds to 4, 3.49 rounds to 3).Code:hitsmax = (int)( ((1 / speed) * 3000) + 0.5 );
That's why I add 0.5 to the value before I truncate it, it pushes x.y to (x + 1).z when y >= 0.5, thus truncating gives x + 1, and it pushes x.y to x.z when y < 0.5, thus truncating gives x.TheOutkastDev said:Casting to less precise numeric data type causes truncation, not rounding. That's why there is a Math.Round() function.
Sep102 said:Would you have preferred I wrote it as "round" instead of round?
Alright, i've tried the adding 0.5 idea and i've checked for a round function (Round, Math.Round, math.Round, and ditto with "round", not "Round"... all don't work (i was too lazy to check)). The adding 0.5 idea I believe I am doing wrong. Here's what I got going...Sep102 said:That's why I add 0.5 to the value before I truncate it, it pushes x.y to (x + 1).z when y >= 0.5, thus truncating gives x + 1, and it pushes x.y to x.z when y < 0.5, thus truncating gives x.
And, yes, there is a Math.Round(), however most of his examples of code earlier were all C++, thus I gave an answer that would work in a managed or unmanaged environment.
Would you have preferred I wrote it as "round" instead of round?
int hitsmax = 0, hitsmin = 0, maxdamage = 0, mindamage = 0, weight = 0;
double t_hitsmax = 0, t_hitsmin = 0, t_maxdamage = 0, t_mindamage = 0, t_weight = 0.0;
t_hitsmax = (1 / wep[sub].speed) * 3000; //sub would be 0, and this is equal to 37
t_hitsmin = t_hitsmax - 30;
t_maxdamage = (wep[sub].strreq + wep[sub].speed + t_hitsmax) / 20; //strreq is 35, speed is 37
t_mindamage = t_maxdamage - ((1 / wep[sub].speed) * 150); //speed is 37
t_weight = (t_maxdamage + t_mindamage) / 2;
hitsmax = (int)(t_hitsmax + 0.5);
hitsmin = (int)(t_hitsmin + 0.5);
maxdamage = (int)(t_maxdamage + 0.5);
mindamage = (int)(t_mindamage + 0.5);
weight = (int)(t_weight + 0.5);
t_hitsmax = (1 / wep[sub].speed) * 3000;
t_hitsmin = t_hitsmax - 30;
t_maxdamage = (wep[sub].strreq + wep[sub].speed + t_hitsmax) / 20;
t_mindamage = t_maxdamage - ((1 / wep[sub].speed) * 150);
t_weight = (t_maxdamage + t_mindamage) / 2;
hitsmax = floor(t_hitsmax + 0.5);
hitsmin = floor(t_hitsmin + 0.5);
maxdamage = floor(t_maxdamage + 0.5);
mindamage = floor(t_mindamage + 0.5);
weight = (t_weight); //weight in UO is a double...