Working with pennies in C

Asked

Viewed 111 times

0

In the code below I can receive values in the pay fields without cents (example: 1,800), but if I put (1,800,20) has error. Detail I’m using setlocale(LC_ALL,"Portuguese"); .

printf("\nAntepenúltimo salário: ");
scanf("%f", &AntepenultimoSalario);

printf("\nPenúltimo salário: ");
scanf("%f", &PenultimoSalario);

printf("\nÚltimo salário: ");
scanf("%f", &UltimoSalario);

MediaSalario  = (UltimoSalario + PenultimoSalario + AntepenultimoSalario) / 3 ;

if(tempoServico >= 6 && tempoServico <= 11){
    printf("Ira receber 3 parcelas");
}else if(tempoServico >= 12 && tempoServico <= 23 ){
     printf("Ira receber 4 parcelas");
}else if(tempoServico >= 24 ){
     printf("Ira receber 5 ");
}else{
    semdireito();
}

// printf("Media Salario: %f", MediaSalario);

if(MediaSalario < 1.22277){
    SalarioFinal = (MediaSalario * 0.8) * 1000 ;
    printf(" parcelas : %5.3f\n", SalarioFinal);
    system("pause");
}else if(MediaSalario > 1.22278 && MediaSalario <= 2.03815){
    SalarioFinal2 = (MediaSalario - 1.22278) * 1000;
    SalarioFinal = (SalarioFinal2 * 0.5) + 978.22;
    printf(" parcelas de R$ %2.f\n", SalarioFinal);
    system("pause");
}else if(MediaSalario >= 2.03815){
    printf(" parcelas de R$ 1.385,91\n");
    system("pause");
}
  • Related: https://answall.com/q/219211/64969

  • Look at my answer: https://answall.com/a/219465/64969, has explanation on monetary calculations using fixed point (BigDecimal and related) and floating point. I also mention a great Infiniteseries video about floating point numbers

  • I need specific guidance in the code above, please

  • not use float for monetary units?

  • Utilise double in the case @Jeffersonquesado ?

  • Neither. It has the same problems. See the above-mentioned answer for more criticism of the use of this type of data.

  • As I mentioned in the question I am beginner and need help applied in the code above

  • That is why I am strongly recommending to understand criticism regarding the inappropriate use of a type of data formulated for scientific applications in contexts that are extremely sensitive to precision.

  • I understood that this incorrect use float, but I don’t know which one to use

  • Fixed point notation. Decimal of arbitrary size. The most common options are these. Fixed point may not require structure, but beware of handling.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.