-2

I made a calculator and it is showing result in many decimal places. How can i restrict it to just two decimal places.

I tried using .tofixed(3) but it is not working.

Working Fiddle: http://jsfiddle.net/m3hfb37x/

YOU
  • 1,030
  • 1
  • 7
  • 22

1 Answers1

2

Try .toFixed(2).

Note that it's 2 (ie, two decimal places) where as your example had 3 for some reason.

Also note the capital F in toFixed. Your example shows a lowercased one which would have resulted in an error.

Changing just this line

$('#amount').text("$" + result_maker);

to

$('#amount').text("$" + result_maker.toFixed(2));

worked for me.

Shadow
  • 8,749
  • 4
  • 47
  • 57
  • O_o It actually Worked! – YOU Feb 20 '18 at 04:10
  • Haha don't be too surprised :) You were pretty close. – Shadow Feb 20 '18 at 04:11
  • One question. How can we make output to show comma/separator in 1000? like 1,000 – YOU Feb 20 '18 at 16:17
  • https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript covers that pretty well. Never be afraid to search for a solution before asking here, this was very easy to find :) – Shadow Feb 21 '18 at 22:06