Turn the value into String for Integer

Asked

Viewed 68 times

-2

I need to fix this mistake:

"Can not deserialize value of type java.lang.Integer

from String "R$5,000.00": not a Valid Integer value".

I need to transform the string parameter (R$ 5,000.00) to integer (5000).

Treatment I started:

var valorAplicacao = vm.valor.replace('R$ ', '').split(".").join("").replace(',', '.');
valorAplicacao = parseFloat(valorAplicacao);    
  • The error ta kind of clear, there is no integer value being passed, but a string. "R$" is not number.

  • Another thing, will the result always be an integer? Ex: if the string is R$ 5,23, the result will be whole (5) or float (5.23)?

  • The error seems to be java but the code seems to be javascript. Confirm this situation in your question.

  • Guys, Ricardo Pontual’s solution is the most viable. It worked normally.

2 answers

0


Your code is almost right, but let’s think about what we should do:

  • Remove the "R$";
  • Remove the ".";
  • Replace the comma with ".";
  • Convert to whole;

In that order, it would look like this:

var numString="R$ 5.000,00";
var numInteiro = parseInt(numString.replace('R$', '').replace('.','').replace(',', '.'));

console.log(numInteiro);

  • Ricardo, the script I posted was already returning the entire value. To keep the value literally whole, I put your suggestion parseint(...). Thanks for the support.

  • Got it. I used the parseInt to be sure. I just did not understand why they went negative the question and the answers, without taking the trouble to leave a comment.

  • Your solution is ok! So I informed as valid solution. I don’t know why they did it. Thank you!

0

Your monetary value has decimal places, but you explicitly said you want to convert to whole, so my suggestion ignores the decimal places of your string:

var numString  = "R$ 5.000,00";
var numInteiro = numString.replace(/[^\d,]/g, '').split(',')[0];

console.log(numInteiro); // Saída: 5000

Browser other questions tagged

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