Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda