We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you plan to complete this in node here is a small tutorial:
Typecasting
We use parseInt() function to parse to integer and parsefloat to parse the double number. Number() is also another function you can use
letstr="123.45";letint1=parseInt(str);// int1 will be 123letstr2="abc123";letint2=parseInt(str2);// int2 will be NaN (Not a Number)lethexStr="0xF";
Precision
In JavaScript, the Number.prototype.toFixed() method is commonly used to control the precision of a number after the decimal point.
This method formats a number using fixed-point notation and returns a string representation of the number with a specified number of digits after the decimal point.
constpreciseNum=num.toFixed(2);// "123.46" (rounds to 2 decimal places)constpaddedNum=num.toFixed(5);// "123.45679" (rounds to 5 decimal places)constzeroDecimals=num.toFixed(0);// "123" (rounds to nearest int``**Solution**// Declare second integer, double, and String variables.varv2,doub,str;// Read and save an integer, double, and String to your variables.v2=parseInt(readLine());doub=parseFloat(readLine());str=readLine();// Print the sum of both integer variables on a new line.console.log(i+v2);// Print the sum of the double variables on a new line.console.log((d+doub).toFixed(1));// Concatenate and print the String variables on a new line// The 's' variable above should be printed first.console.log(s+str);eger)
Day 1: Data Types
You are viewing a single comment's thread. Return to all comments →
If you plan to complete this in node here is a small tutorial: Typecasting We use parseInt() function to parse to integer and parsefloat to parse the double number. Number() is also another function you can use
Precision In JavaScript, the Number.prototype.toFixed() method is commonly used to control the precision of a number after the decimal point. This method formats a number using fixed-point notation and returns a string representation of the number with a specified number of digits after the decimal point.