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

154
Visualizações
parseFloat whole number with two decimal points -Javascript

Below i have a use case, where a number is stored in db as decimal(18, 2). When trying to read the data, the numbers with .00 are converted to integer.

Using .toFixed(2) changes the format from number to string. // as expected.

How can i retain the format.

Input

    const x1 = 4000.00;
    const x2 = 4000.01;

    const y1 = parseFloat((x1).toFixed(2)); // actual: 4000 expected: 4000.00
    const y2 = parseFloat((x2).toFixed(2)); // actual: 4000.01 expected: 4000.01

    // const y1 = parseFloat((x1).toFixed(2)).toFixed(2); //datatype: actual: string, expected: number 
    // const y2 = parseFloat((x2).toFixed(2)).toFixed(2); //datatype: actual: string, expected: number

    console.log(typeof(y1), y1);
    console.log(typeof(y2), y2);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

decimal(18,2) is a fixed-precision number format, here meaning "18 decimal digit, with 2 of them to the right of the decimal point". Typically stored as either packed decimal or an integer. with precision and scale maintained by the programming language.

Javascript numbers are not like that. They are IEEE 754 double-precision binary floating point numbers: they have a floating decimal (binary) point, and trade precision for range.

You need to read What Every Programmer Should Know About Floating-Point Arithmetic, or Why don’t my numbers add up?.

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