geef
geef

Reputation: 589

Rounding when using Math.(sin)

var PI = 3.14159265358979323846;
var intelect = 33;
var ageInDays = 54

document.write("answer: " + Math.sin(ageInDays*2*PI/intelect));

http://jsfiddle.net/aRzRc/

Is it possible to round when using Math.sin?

Upvotes: 1

Views: 172

Answers (3)

Boldewyn
Boldewyn

Reputation: 82794

What hinders you to use Math.round (or Math.floor, Math.ceil)?

var PI = 3.14159265358979323846;
var intelect = 33;
var ageInDays = 54

document.write("answer: " + Math.floor(Math.sin(ageInDays*2*PI/intelect)));

http://jsfiddle.net/aRzRc/1/

Upvotes: 0

Nivas
Nivas

Reputation: 18354

You are looking for toFixed:

Math.sin(ageInDays*2*PI/intelect).toFixed(2)); //Ans: 0.76

Upvotes: 6

TJHeuvel
TJHeuvel

Reputation: 12618

Sure you can;

var calc = Math.sin(ageInDays*2*Math.PI/intellect);    
document.write(Math.round(calc));

Or, alternatively:

document.write("answer: " + Math.round(Math.sin(ageInDays*2*PI/intellect)));

There is no need to define your own PI, javascript's math object has a property for that.

Upvotes: 1

Related Questions