The first thing you should do is get the whole part of the episode and the decimal part of the episode and add them each in separate accumulators. Then the sum of the decimal part or thirds should verify if it is divisible by 3, but all this can be done with a simple formula.
double[] entradas = { 0.2, 0.1, 5.2, 3.1 };
double totalEpisodiosJugados = 0.0;
int episodiosCompletos = 0;
int terciosCompletos = 0;
int episodio = 0;
int tercio = 0;
for (int i = 0; i < entradas.Length; i++)
{
episodio = (int)Math.Truncate(entradas[i]);
tercio = (int)(((entradas[i] - episodio) + 0.05) * 10);
episodiosCompletos += episodio;
terciosCompletos += tercio;
}
totalEpisodiosJugados = episodiosCompletos + Math.Truncate(terciosCompletos / 3D) + ((terciosCompletos % 3) / 10D);
Console.WriteLine(string.Format("Episodios: {0}\nTercios: {1}\nTotal: {2}", episodiosCompletos, terciosCompletos, totalEpisodiosJugados));
Console.ReadKey();
The example that I leave to you is extremely easy, inside the loop the only thing that is done is to obtain the integer values of the episode and add them. The same is done with the thirds with the difference being multiplied by ten (10) to obtain the whole value of the decimal point of the third. At the end you get the sum of both the episodes and the thirds.
Now the important part is the total calculation of the episodes:
episodiosCompletos
+ Math.Truncate(terciosCompletos / 3D) + ((terciosCompletos % 3) / 10D)
Episodes previously calculated within the loop.
episodiosCompletos +
Math.Truncate(terciosCompletos / 3D)
+ ((terciosCompletos % 3) / 10D)
Now divided by 3 the number of thirds and the whole value is extracted from it, with this we get the number of episodes that are within the thirds.
episodiosCompletos + Math.Truncate(terciosCompletos / 3D) +
((terciosCompletos % 3) / 10D)
This part is important, since, if the sum of the thirds is not a divisible value between 3, then the rest of the division is obtained by 3 to obtain the third of the remaining episode, but as an integer is returned, it is divided by 10.
The same applies if you pass it to an excel.
Result:
Formulas: The same calculations that are made in C #. Note that the function RESIDUO
is used in Excel (Its counter part in English is MOD) and in C # the percentage symbol ( %
) to obtain the rest of the division.
Entradas Episodio Tercio
0.2 =TRUNCAR(A2) =(A2-B2)*10
0.1 =TRUNCAR(A3) =(A3-B3)*10
5.2 =TRUNCAR(A4) =(A4-B4)*10
3.1 =TRUNCAR(A5) =(A5-B5)*10
=SUMA(B2:B6) =SUMA(C2:C6) =B7 + TRUNCAR(C7/3) + (RESIDUO(C7,3) / 10)
Episodios C. Tercios C. Total Episodios Jugados
Updating:
As I say in the comments (I do not program in php), but, the question arose because it gives you decimals, so look on the internet how you create a array
, how to use a for
and what is the counterpart of Math.Truncate
in php, so I turned to a php fiddle online
and managed to do this. So far I am working well.
<?php
$entradas = array(0.2, 0.1, 5.2, 3.1);
$totalEpisodiosJugados = 0.0;
$episodiosCompletos = 0;
$terciosCompletos = 0;
$episodio = 0;
$tercio = 0;
for($i = 0; $i < count($entradas); $i++){
$episodio = intval($entradas[$i]);
$tercio = ($entradas[$i] - $episodio) * 10;
$episodiosCompletos += $episodio;
$terciosCompletos += $tercio;
}
$totalEpisodiosJugados = $episodiosCompletos + intval($terciosCompletos / 3) + (($terciosCompletos % 3) / 10);
echo "Episodios: ", $episodiosCompletos, ", ";
echo "Tercios: ", $terciosCompletos, ", ";
echo "Total: ", $totalEpisodiosJugados;
?>