Decimal output needed when dividing integers [duplicate] - c#

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have 2 input int values that need do be divided and sen back as a string with 6 decimals.
int a = 240
int b = 1440.
I want to divide them and send back a text string with 0,166667
I have tried many code examples but none have worked.

You need to convert at least one to a decimal value:
double result = (double)a / b;
or
decimal result = (decimal)a / b;
On "decimal vs. double" see THIS question.

Related

Prevent rounding off in string.format [duplicate]

This question already has answers here:
C# Double - ToString() formatting with two decimal places but no rounding
(15 answers)
Closed 8 months ago.
string xyz = "23.659";
return string.Format("{0:F"+2+"}", xyz);
output = 23.66
When I use string.format the value gets rounded off, but I need 23.65 as the output.
How to prevent rounding off in string.format?
You can round the value manually, e.g. with a help of Math.Round:
// If you have string (not decimal or double) we'll have parse
string xyz = "23.659";
return double.TryParse(xyz, out var value)
? string.Format("{0:F2}", Math.Round(value, 2, MidpointRounding.ToZero))
: xyz;
If xyz is of type float, double, decimal which is more natural the code doesn't want parsing:
double xyz = 23.659;
return string.Format("{0:F2}", Math.Round(xyz, 2, MidpointRounding.ToZero));

Why can't I convert an integer into binary with Convert.ToInt32? [duplicate]

This question already has answers here:
Convert integer to binary in C#
(22 answers)
Closed 3 years ago.
By using that statement I can change my binary value to decimal value by swapping the values but why isn't this statement working for decimal to binary conversion?
int decimalVal = 10;
int binaryVal = Convert.ToInt32(decimalVal, 2); // want 1010
You should use Convert.ToInt32(String, Int32) method to convert binary to integer and Convert.ToString(Int32, Int32) method to convert integer to binary.
string binaryVal = Convert.ToString(decimalVal, 2);
To find more information:
https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=netframework-4.8
https://learn.microsoft.com/en-us/dotnet/api/system.convert.tostring?view=netframework-4.8
int decimalVal = 10;
string binary = Convert.ToString(decimalVal, 2);
They are of different byte sizes: https://condor.depaul.edu/sjost/nwdp/notes/cs1/CSDatatypes.htm And you are trying to convert one directly and expect binary while this does not work like that.
This two answers should help you:
How do I convert a decimal to an int in C#?
And
Convert integer to binary in C#
What you will figure out from this two answers is that Covnert.ToInt32 does not have an overload that takes an instance of decimal and converts it to binary. You will need to cast the decimal first to an integer and then you would be able to cast it to binary, For example:
decimal value = 8;
int n = Convert.ToInt32(value);
string binary = Convert.ToString(n, 2);
binary.Dump();
The output would be: 1000
This example is tested in Linqpad

How to Convert Hex to Double C#? [duplicate]

This question already has answers here:
Convert Hex to Double
(4 answers)
Closed 6 years ago.
i have hex data "44 34 00 00" , and i'll Convert to double "720.0" using C#
like this..
anyone can help? thanks..
What you have is the bytes from a Single (aka float), so do this:
double result = BitConverter.ToSingle(BitConverter.GetBytes(0x44340000), 0);
Console.WriteLine(result); // Prints 720
check that
https://msdn.microsoft.com/en-us/library/system.bitconverter.int64bitstodouble
BitConverter example:
double d = BitConverter.Int64BitsToDouble(0xdeadbeef);

decimal rounding to 2 precision such that X is converted to X.00 C# [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
First of all this is not a duplicate question my need is to have 2 precision decimal always
e.g.
2 should be converted as 2.00
0 should be converted as 0.00
0.5 should be converted as 0.50
output number has to be decimal
Things i had tried
decimal val = 0.5000M
decimal d = Math.Round(val, 2);//gives 0.5 i need 0.50
NOTE: I am not looking to convert decimal to string as i need to send it as a soap request which accepts decimal value only, so i dont have flexibility to convert it to string.
Update
below code as answered by Jakub Lortz works fine though it may look complex
solution 1 by Jakub Lortz
decimal twoPoint00 = new Decimal(200, 0, 0, false, 2);
decimal twoPoint0 = new Decimal(20, 0, 0, false, 1);
Console.WriteLine(twoPoint00) // prints 2.00
Console.WriteLine(twoPoint0) // prints 2.0
solution 2 by Bogdan
he had given very simple solution which will also work
decimal val = 0.5000M;
string result = string.Format("{0:f2}", val);
decimal d;
Decimal.TryParse(result, out d);
There is a constructor of Decimal that allows you to specify the scale:
public Decimal(
int lo,
int mid,
int hi,
bool isNegative,
byte scale
)
For example:
decimal twoPoint00 = new Decimal(200, 0, 0, false, 2);
decimal twoPoint0 = new Decimal(20, 0, 0, false, 1);
Console.WriteLine(twoPoint00) // prints 2.00
Console.WriteLine(twoPoint0) // prints 2.0
This constructor is used by the C# compiler to create decimals from literals, so you can easily check it in the compiled IL.
Edit
To convert a decimal value to a representation with a set precision, you would first have to use Decimal.GetBits method to get its internal representation. Then modify the values (maybe using BigInteger to work with the whole 96-bits value as a whole?) and create a new decimal.
Seems like a lot of job, with a lot of possible bugs. I'd start by checking the solution proposed in Bogdan's answer - ToString() + Parse(). It seems to work correctly and is very simple.
I guess you want to convert the decimal into a string. Try it like that:
string result = d.ToString("#.00");
decimal val = 0.5000M;
string result = string.Format("{0:f2}", val);
decimal d;
Decimal.TryParse(result, out d);
#Jakub Lortz - Thank you for the suggestions:
decimal val = 0.5000M;
string formattedVal = val.ToString("F2");
decimal result = Decimal.Parse(formattedVal);

Convert string to same hex value in c# [duplicate]

This question already has answers here:
How to parse hex values into a uint?
(4 answers)
Closed 7 years ago.
I have a string varaible str. This variable has a value "26". Can I convert it into hex that results 0x26 not 0x1A.
Means str="26"
int iConvert=**SomeConvertionFunction(str)**
should result into iConvert=0x26
int hex = int.Parse("26", NumberStyles.HexNumber);
hex value will be 38 which is the decimal representation of hex number 0x26.
If I understand your question basically you want to store the Hex value of the string in a int?
having the value 26 or 0x1A is the same.
To convert the int to a string use int.ToString('X');
And please not that 26 in base 10 in equal to 0x1A in base 16.

Categories