Comments (11)
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.
Yes I need only two digits after period
from jslt.
Do you understand the question?
from jslt.
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.
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.
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.
Is there a solution for this issue? All i want is to format the value to have two digits after the decimal.
from jslt.
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.
Great! Yes this what I want. Thank you.
from jslt.
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.
Thank you
from jslt.
Related Issues (20)
- Built-in sha256-hex function is not thread-safe HOT 6
- Unable to retrieve the values of the nested loops using JSLT HOT 1
- Pipe Operator not affecting Context HOT 3
- Complex Nesting For loop issues HOT 3
- Support for processing nested JSON
- Determine index of current item in object array HOT 3
- JsltJsonFilter clarification
- Variables, Scope and Import HOT 10
- Pass values from parent to imported jslt HOT 3
- I want to call external java method in jslt during transformation, is it possible??? HOT 3
- How to set counter value after each iteration of for loop in jslt HOT 6
- How to compare two arrays and get unique value HOT 4
- Is it possible to retrieve the common values from two arrays?i.e the values which are available in both the arrays HOT 2
- How to push elements to an array HOT 6
- Can u fix sum function precision overflow bug when double addition integer HOT 8
- Can enhance matcher expression to operate on a target specified with dot expression? HOT 2
- Need help with transforming by adding root fields to child array elements HOT 1
- json key have space HOT 1
- Filter out all keys with a specific name from all objects HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jslt.