Fixed precision always in js 0.5 -> 0.5000

Fixed precision always in js 0.5 -> 0.5000



I am trying to fix every fraction number up to n decimal place. But in javascript/nodejs it seems not possible to convert 0.5 to 0.5000. Due to which my test cases which expect 0.5000 fails.


0.5


0.5000


0.5000



Any idea how can I do this in nodejs/javascript?





What part of .toFixed(4) isn't working?
– Niet the Dark Absol
Aug 19 at 13:30


.toFixed(4)





Why do you need to test it against0.5000 in the test case. Either you use a decimal class to work with real decimal numbers. Or you have floating point numbers, and then 0.50000 does not make any sense anyway. But even if you have a decimal class then why should 0.50000 be relevant?
– t.niese
Aug 19 at 13:32



0.5000


0.50000


0.50000





@t.niese Please have a look at this hackerrank.com/challenges/plus-minus/… Their check is failing since my js code returns 0.5 and not 0.5000
– Suresh Prajapati
Aug 19 at 13:35





@t.niese in case you want to see what I tried function plusMinus(arr) var result = [0,0,0]; for(var i = 0; i < arr.length; i++) if(arr[i] > 0) result[0] += 1; else if(arr[i] < 0 ) result[1] +=1; else result[2] +=1; for(var i = 0; i < result.length; i++) result [i] = (result[i] / arr.length); return result;
– Suresh Prajapati
Aug 19 at 13:38


function plusMinus(arr) var result = [0,0,0]; for(var i = 0; i < arr.length; i++) if(arr[i] > 0) result[0] += 1; else if(arr[i] < 0 ) result[1] +=1; else result[2] +=1; for(var i = 0; i < result.length; i++) result [i] = (result[i] / arr.length); return result;





Add relevant code into your question and not as comment. There is neither a decimal number, nor a floating point number or any test case in that code.
– t.niese
Aug 19 at 13:40




2 Answers
2



As others said Number.prototype.toFixed() should work for you. I tried my hands on and this works perfectly.


Number.prototype.toFixed()




function plusMinus(arr)
let length = arr.length;
let o = arr.reduce((acc,cv)=>
if(cv > 0)
acc.pos++;

else if(cv<0)
acc.neg++;

else
acc.zero++;

return acc;
,pos:0,neg:0,zero:0);
console.log((o.pos/length).toFixed(4));
console.log((o.neg/length).toFixed(4));
console.log((o.zero/length).toFixed(4));

plusMinus([-4,3,-9,0,4,1]);





Can you please check the test cases result for your solution here hackerrank.com/challenges/plus-minus/…
– Suresh Prajapati
Aug 19 at 14:10






I don't know why but i submitted with this code and it works :P
– vibhor1997a
Aug 19 at 14:11





Great!! It worked...
– Suresh Prajapati
Aug 19 at 14:12





Only part which was required is to print those. Now it's working for my code too
– Suresh Prajapati
Aug 19 at 14:15





@SureshPrajapati It should have thats what I commented earlier.
– vibhor1997a
Aug 19 at 14:16



Use the number prototype method toFixed


number


toFixed




var num = 5.0;
var n = num.toFixed(4);
console.log(n) //output: 5.000





This return a string and not a number
– Suresh Prajapati
Aug 19 at 13:33





@SureshPrajapati in js you only have floating point numbers, there is nothing like 0.5000. If you want to have 0.5000 then you need to use a string (or a decimal class).
– t.niese
Aug 19 at 13:34



0.5000


0.5000





@SureshPrajapati but the output that represents 0.5000 has to be always a string. There is no numeric type that represents 0.5000
– t.niese
Aug 19 at 13:37



0.5000


0.5000





@SureshPrajapati I think the string shouldn't be a problem in your use case.
– vibhor1997a
Aug 19 at 13:43





@SureshPrajapati You need to print the answer in the stdout. So, if you use toFixed then you can print that into console. That shouldn't be an issue.
– vibhor1997a
Aug 19 at 13:51


toFixed






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj