0

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
*/
2
  • Possible duplicate of Double bitwise NOT (~~) in C# Commented Jun 16, 2018 at 3:18
  • @cwharris I don't think so, the title is promising but the contents of that QA are unrelated to this question Commented Jun 16, 2018 at 11:16

1 Answer 1

1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.