Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am printing Simple Interest and taking a string with space separated values such as
"1500 1.4 3"
.

I want my result in typeOf Number preceded with 6 Decimal places.
such as
63.000000


I have written a code for this in JavaScript.

JavaScript
<pre>const solve = (input) => {
	// Write your code here
	input=input.split(" ").map((x)=>parseFloat(x));
	let result=((input[0]* input[1]* input[2])/100).toFixed(6);
	console.log(result);
}

solve("1500 1.4 3")


What I have tried:

toFixed is working as expected but when I need to print result in NUMBER format not in string. 

and when I am using <pre lang="Javascript">parseFloat(), Number()

It removes all the extra 0s but I need them.
How to print Number with 6 Preceding decimal places.
Posted
Updated 4-Aug-22 18:22pm

1 solution

Numbers don't have "trailing zeros" - they aren't even stored as decimal values but in binary instead to the whole concept of "6 decimal places" doesn't actually work! You cannot "force" a number to have a certain number of decimal places except when you convert it to a string using the JavaScript toFixed() Method[^] as you have seen.

Basically, when you want to print a number - any number - it gets converted to a string, and that's when you specify how many decimal places it should have.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900