Giter Site home page Giter Site logo

Comments (11)

larsga avatar larsga commented on June 3, 2024

Your example is not rounding, because the two numbers are equal. Are you asking how to display it in a string with two decimals after the period?

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Yes I need only two digits after period

from jslt.

larsga avatar larsga commented on June 3, 2024

Do you understand the question?

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Yes I understand. I don't want to round off the value. I have a value like 40.0000 I wanted to trim it upto 2 digits after period. I. e 40.00

from jslt.

larsga avatar larsga commented on June 3, 2024

This question is a bit frustrating, because I've had it before, but I've never been able to fully figure out what it is people are asking for.

Sometimes they may actually want rounding, like here. Or maybe not. It's hard to tell.

This guy, I think, wanted the number formatted inside a string in a particular way.

Although you don't say so, I suspect it's the formatting inside a string that you want, too. Am I right?

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Yes I referred above two examples. but I don't get the solution.I don't want the round off, instead I wanted to format value upto 2 digits after the period.

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Is there a solution for this issue? All i want is to format the value to have two digits after the decimal.

from jslt.

catull avatar catull commented on June 3, 2024

It depends what you want to achieve.

If it is being processed elsewhere, then "55.0", "55.00" and "55" are all the same value.

But do you display it on an UI ?

Now, knowing almost nothing about your requirements, here is an attempt.

Take this input:

{
  "values": [
    55,
    55.0,
    55.00,
    39.585,
    13.995,
    53.7745
  ]
}

with this transformation:

def formatNumberTotwoDecimals (num)
  let fix      = floor ($num)
  let partial  = floor (100 * ($num - $fix))
  let dezi     = floor ($partial / 10)
  let centi    = mod ($partial, 10)
  let afterDot = join ([$dezi, $centi], "")
  join ([$fix, $afterDot], ".")

{
  "values": [
    for (.values)
      formatNumberTotwoDecimals (.)
  ]
}

you obtain:

{
  "values" : [ "55.00", "55.00", "55.00", "39.58", "13.99", "53.77" ]
}

Is this what you want ?

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Great! Yes this what I want. Thank you.

from jslt.

catull avatar catull commented on June 3, 2024

Hang on, I made a mistake.

Here is a more precise version of the transformation.

def formatNumberToTwoDecimals (num)
  let fix      = floor ($num)
  let partial  = floor (100 * ($num - $fix))
  let dezi     = floor ($partial / 10)
  let centi    = mod ($partial, 10)
  let afterDot = join ([$dezi, $centi], "")
  join ([$fix, $afterDot], ".")

{
  "values": [
    for (.values)
      formatNumberToTwoDecimals (. + 0.005)
  ]
}

With input:

{
  "values": [
    55,
    39.585,
    13.995,
    53.7745
  ]
}

the result would be:

{
  "values" : [ "55.00", "39.59", "14.00", "53.77" ]
}

Hope this helps.

from jslt.

joel19913 avatar joel19913 commented on June 3, 2024

Thank you

from jslt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.