user1633912
user1633912

Reputation: 1

convert javascript double bitwise not to c#

How to convert a double value with double bitwise not in c#. I have method javascript use double bitwise

var i= 0.0008590;
var j= 0.000002;
~~(i / j) * j
console.log(i );
/*
i=0.0008579999999999999
*/

Upvotes: 0

Views: 62

Answers (1)

user555045
user555045

Reputation: 64903

The double complement in JavaScript is a trick to convert a value to a 32bit integer, with fractional values being truncated (not ceiling or floor, but rounded towards zero). C# has an explicit syntax for such a conversion: (int)value.

var i = 0.0008590;
var j = 0.000002;
var result = (int)(i / j) * j;
Console.WriteLine(result);

The result is approximately 0.000858

Or you could leave out the type conversion aspect and merely truncate:

var i = 0.0008590;
var j = 0.000002;
var result = Math.Truncate(i / j) * j;
Console.WriteLine(result);

An advantage is that it does not have funny behaviour for inputs that result in i / j being outside of the range of an integer.

Upvotes: 1

Related Questions