How to remove value after floating point

Asked

Viewed 57 times

1

I have the code section exposed below:

var calcularSoma = function (primeiroValor, segundoValor) {
var soma = primeiroValor + segundoValor;
if (soma < 10) {
	console.log(soma, "Unidades.");
	return soma;
} else if (soma < 100) {
	var restoUnidade = soma % 10;
	var dezena = soma / 10;
	if (restoUnidade == 1 && dezena <= 1) {
		console.log(dezena, "Dezena. e ", restoUnidade, "Unidade");
	} else {
		console.log(dezena, "Dezenas. e ", restoUnidade, "Unidades");
	}
	return soma;
} else if (soma < 1000) {
	var centena = soma / 100;
	console.log(centena, "Centenas.");
	return soma;
} else {
	var milhar = soma / 1000;
	console.log(milhar, "Milhar.");
	return soma;
}
}

calcularSoma(5, 10);

1. What I intend is to display the result in the plural if the values contained in the variables are greater than 1. I will implement the conditions below, at the moment I need to understand first which implementation will represent the logic exposed.

2. I need to format the displayed value, in this example is displayed the value 1.5 Dezenas e 5 Unidades.(plural Unity, because the rest was greater than 1), wanted to display something like: 1 Dezena e 5 Unidades.

3. Here I used if(restoUnidade == 1 && dezena <= 1) because in this example above the value of the variable ten in thesis is greater than 1, although it is a decimal.

P.S.: If possible tell me how to best expose the doubt, first the code following the doubts (as stated in this post), or the doubts first followed by the code?

  • managed to solve?

1 answer

0

When seeing me you need to add a few more ifs.

The problem may be in if (restoUnidade == 1 && dezena <= 1)

I don’t quite understand what you want, but see if that helps you. Modify this section in the code:

else if (soma < 100) {
  var restoUnidade = soma % 10;
  var dezena = soma / 10;
  if (restoUnidade == 1) {
    if (dezena <= 1) {
      console.log(dezena, "Dezena. e ", restoUnidade, "Unidade");
    } else {
      console.log(dezena, "Dezenas. e ", restoUnidade, "Unidade");
    }
  } else if (dezena > 1) {
    console.log(dezena, "Dezenas. e ", restoUnidade, "Unidade");
  } else {
    console.log(dezena, "Dezena. e ", restoUnidade, "Unidade");
  }
  return soma;
}

Browser other questions tagged

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