RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Convert.ToInt32()

noobie said:
then its already 0 or 0.xx

thats the simplest way of doing it.

Hmm, just checked that. Not true...

It should be 7.65, making it 8.

speed/strreq
37/35
hitsmax = (int)(((1 / speed) * 3000));
maxdamage = (int)(((strreq + speed + hitsmax) / 20));

hitsmax should be 81.08108108... making it 81
It's below 10 appearently, so an If statement makes it 10.

But assuming it WAS 81, then maxdamage should be: 7.65, making it 8.
If it was 0, then it should be: 3.6, making it 4.

Either way, it shouldn't be 0...
 

Sep102

Page
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:
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).

Casting to less precise numeric data type causes truncation, not rounding. That's why there is a Math.Round() function.
 
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).

So, I should make it like...

t_hitsmax = ( ((1 / speed) * 3000) + 0.5 );
hitsmax = (int)t_hitsmax;

correct?
 

Sep102

Page
TheOutkastDev said:
Casting to less precise numeric data type causes truncation, not rounding. That's why there is a Math.Round() function.
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?
 
Sep102 said:
Would you have preferred I wrote it as "round" instead of round?

Yes. Simply because, when you go back to your code, you might forget why you added that 0.5 to your number. Whereas if you see your variable passed to a function "round" you know what the purpose is. Besides, writing a round function gives you something to add to your own personal library for other projects.
 
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?
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...

Code:
    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);

Edit: Wait, I think I got it. I can floor it after I add the 0.5 XD
I think that's what you were saying. lmao *smacks head*
I'll go try this out.

Edit... Still didn't work. I'm guessing it may have to do with them being integers to say the least? (unless I was supposed to floor them in a different location? Like in the t_xxx...)

So, NOW I have this:

Code:
        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...
 
Top