Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Math.Floor problem
Message
De
19/07/2008 11:16:26
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
OS:
Windows XP SP2
Divers
Thread ID:
01332456
Message ID:
01332509
Vues:
6
>>>Can anybody help me to make this piece work?
>>>The problem is that Math.Floor is type of Double and none of casting that I apply to the variables does not work so far. Getting "Cannot implicitly convert type 'double' to 'int' " error all the time.
>>>"
>>>
>>>C#, VS 2003
>>>
>>>{
>>>    int _CurrentPageIndex = 5;
>>>    int _PageButtonCount = 2;
>>>    int StartLoop = 0;
>>>
>>>    StartLoop = (Math.Floor(_CurrentPageIndex / _PageButtonCount) * _PageButtonCount) + 1;
>>>}
>>>
>>
>>(1) There is a confusion in the compiler whether to convert (_CurrentPageIndex / _PageButtonCount) to double or to decimal, since floor can take both double and decimal
>>You can help/hint the compiler by casting either _CurrentPageIndex or _PageButtonCount to a double or decimal
>>
>>(2) the result is an expression of type double or decimal. Assigning that to an int results in a loss of precision. The compiler does not do that for you. Hence you have to cast it to an int
>>
>StartLoop = (int)(Math.Floor((double)_CurrentPageIndex / _PageButtonCount) * _PageButtonCount) + 1;>
>>But maybe this does what you're after also
>>Since CurrentPageIndex and PageButtonCount are int, the result is an integer division with no remainder
>>[ remember - this is not foxpro ]
>>
>>StartLoop = (_CurrentPageIndex / _PageButtonCount) * _PageButtonCount + 1;
>>
>
>Thanks Gregory and Viv!
>
>this:
>StartLoop = (int)(Math.Floor((double)_CurrentPageIndex / _PageButtonCount) * _PageButtonCount) + 1;
>
>seems to work.
>
>Amaizing, how many troubles you may have in .Net to do something very simple which does not present any problem in VFP. :)
>
>Exactly like Rick Strahl used to say: ".Net makes hard things easy and easy things hard..." :-)

_______
You're welcome


btw, does the second solution work without the floor ? It think it should since integer division kind of 'floors' already
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform