Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
Rounding in Cypress

I want to compare a number from a form to a set number. The issue is that we have dozens of different systems and depending on the system the number can be displayed with 2 to 4 decimal places. So in one system the number is displayed as 11,1265, in another as 11,127 and in yet another as 11,13. Therefore I would like to found the number to decimal places before the assertion.

I have already found this this answer and and tried to implement the solution. Could not get it to work though as Cypress does not know .toFixed()

This is what I have tried so far:

  1. From answer 1
cy.get('#Client_rate_ap').then(res => {
                expect(res.toFixed(2)).to.equal('11,13')
})

This returns

TypeError
res.toFixed is not a function
  1. with cy.wrap()
 cy.get('#Client_rate_ap').then(res => {
                cy.wrap(res).toFixed(2).should('be.equal','11,13')
            })

and

 cy.get('#Client_rate_ap').then(res => {
                cy.wrap(res.toFixed(2)).should('be.equal','11,13')
            })

Both also return the error toFixed is not a function

{{edit}} Added element

This is the element with the value that I want to compare:

<input class="rate_ap tariff-connected-field" name="Client[rate_ap]" id="Client_rate_ap" type="text" value="11,1300">
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to first convert your text to float and then apply .toFixed(2). Something like:

cy.get('#Client_rate_ap')
  .invoke('val')
  .then((rate) => {
    expect(parseFloat(rate).toFixed(2)*1).to.equal(11.13)
  })

In case your prices are displayed in comma, then first you have to replace the comma with a decimal, in that case you can:

cy.get('#Client_rate_ap')
  .invoke('val')
  .then((rate) => {
    expect(parseFloat(rate.replace(',', '.')).toFixed(2)*1).to.equal(11.13)
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

The simplest way using the answer you already found,

cy.get('#Client_rate_ap')
  .then(res => {
    expect(Math.abs(11.13 - res.replace(',','.'))).to.be.below(0.01)
  })
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda