Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Math.Floor problem
Message
De
19/07/2008 03:59:33
 
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:
01332470
Vues:
13
This message has been marked as the solution to the initial question of the thread.
>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;
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform