2

I am newbie to javascript, forgive me if i ask this kind of question

in c# I have :

(float)3772/32767 = 0.115115821

but in javascript i have :

3772/32767 = 0.11511581774346141

I heard there is a function parseFloat but it does not fix my problem

I want the result to be like the c# float result, because in javascript side it brings some noise to my final data, how to cast like a c# float ?

1 Answer 1

3

You can use the toFixed() or toPrecision() methods to change the precision of float-numbers:

let number = 3772/32767;

console.log(number);

console.log(number.toFixed(7));

console.log(number.toPrecision(8));

Sign up to request clarification or add additional context in comments.

4 Comments

Yes but I also want the last two digits
No matter what significance you round to it'll never be 0.115115821, since 17 is either rounded to 2 or 177 is rounded to 18
yes I agree, this was just an example, i have thousands of operations , each operation needs its own precision
And since floating point math is very different between c# and js you're unlikely to get the same number when you're talking about billionths

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.