Rounding a value in Typescript

Asked

Viewed 124 times

-1

How do I enter a number, how do I add it in the code (imc calculator in typescript)?. And how do I add it in the code ifs to inform healthy, obese, about weight, etc...

const calculadora = document.querySelector<HTMLFormElement>('#calculadora')!
const peso = document.querySelector<HTMLInputElement>('#peso')!
const altura = document.querySelector<HTMLInputElement>('#altura')!
const mensagem = document.querySelector<HTMLParagraphElement>('#mensagem')!
const retorno = document.querySelector<HTMLParagraphElement>('#retorno')!

function calculoIMC (peso: number, altura: number) {
  return peso / (altura*altura)
  
}

calculadora.addEventListener('click', (e: Event ) => {
    e.preventDefault()
    let novoPeso = parseFloat(peso.value)
    let novaAltura = parseFloat(altura.value)
    mensagem.innerText = "Seu IMC e de :" + (calculoIMC (novoPeso, novaAltura))
})
  • 2

    Do not use images of your code, post it in the question. It is easier to help. See these tips for better guidance.

  • const calculator = Document.querySelector<Htmlformelement>('#calculator')! const peso = Document.querySelector<Htmlinputelement>('#weight')! const height = Document.querySelector<Htmlinputelement>('#height')! const message = Document.querySelector<Htmlparagraphelement>('#message')! const return = Document.querySelector<Htmlparagraphelement>('#return')! Function calculoIMC (weight: number, height: number) { Return weight / (height*height) }

  • calculadora.addEventListener('click', (e: Event ) => {&#xA; e.preventDefault()&#xA; let novoPeso = parseFloat(peso.value)&#xA; let novaAltura = parseFloat(altura.value)&#xA; mensagem.innerText = "Seu IMC e de :" + (calculoIMC (novoPeso, novaAltura))&#xA;})

  • Okay, Victoria, I see you’re starting to use the site now. No problem I still don’t know how to post the code, but I made an edition of your question, and I highlighted your code. See how it was for you to base yourself in the future. It’s always good to post the code on yourself and not images of it, okay? D

  • 2

    This answers your question? Round a number to Top 4023.8599999999997€

  • 1

    has several questions/answers here on the site of how to round up using javascript

  • 1

    Besides, the question isn’t about Typescript. Since rounding is done in Runtime (and not in Compile time), it’s about Javascript...

Show 2 more comments

1 answer

0


Using simple Javascript/Typescript methods, provided by the class Math, you can round up or down any non-integer number.

Behold:

function calculoIMC (peso, altura) {
  return peso / (altura*altura) 
}

console.log(calculoIMC(80.2, 1.78));

console.log(Math.ceil(calculoIMC(80.2, 1.78)));

console.log(Math.floor(calculoIMC(80.2, 1.78)));

See that Math.ceil round up, while Math.floor round down.

These same methods work in Typescript, just put as pure JS to be able to exemplify in the above code.

Your code would look like this:

calculadora.addEventListener('click', (e: Event ) => {
    e.preventDefault()
    let novoPeso = parseFloat(peso.value)
    let novaAltura = parseFloat(altura.value)
    mensagem.innerText = "Seu IMC e de :" + Math.floor(calculoIMC (novoPeso, novaAltura)) // ou Math.ceil
})
  • thank you very much! could you tell me how I validated to enter the values correctly? and as add if for example (if (parseFloat(imc) > 18) { return.inertext = ""}

Browser other questions tagged

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