Float calculation showing difference. Positive showing negative? [duplicate] - c#

This question already has answers here:
Always return positive value
(9 answers)
Closed 5 years ago.
I am calculating the difference between two numbers. If the calculation ends up being 5 - 10, it equals to "-5". If this is the case I need results to display/equal to "+5" , with the "+" sign.
I basically need reverse. So same if 10 - 5 quals to "5" I need it to display as "+5"
Code below I am using:
float rowresults = ROW1 - ROW2;
Textbox.text = rowresults.ToString();

Math.Abs is what you are looking for:
float rowresults = Math.Abs(ROW1 - ROW2);
And to add the "+"-sign to the front of the text (without changing your elsewise existing behaviour):
Textbox.text = "+" + rowresults.ToString();

Related

Incorrect calculation in C# [duplicate]

This question already has answers here:
C# Getting strange results for a simple math operations
(4 answers)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Why (0.406 * 10000.0) returns 4060.0000000000005 instead of 4060.0 in C#
I have written a function which checks no. of decimals in a double value and below is the code I am using. The problem described in the above sentence occurs when value of d is 0.406 and values of n is 4 and the function returns true instead of false
I am open to using alternate solution.
public static bool HasMoreThanNDecimals(double d, int n)
{
return !(d * (double)Math.Pow(10, n) % 1 == 0);
}
Just use decimal type instead of double for more precision to get the desired result.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral

C# increment a variable and convert to string on one line [duplicate]

This question already has answers here:
Pre- & Post Increment in C#
(6 answers)
Closed 5 years ago.
Simple request -- does anybody know a way to consolidate this to one line:
edit - I was NOT confused about pre vs post increment operators. The question I posted should have worked, I honestly do not know what happened. somewhere between VS and SO I fixed it.
startingMask++;
string mask2 = startingMask.ToString().PadLeft(3, '0');
Something like
string mask2 = (startingMask++).ToString().PadLeft(3, '0');
edit -- Alright -- chill out -- :)
Here is the final full solution, I should have provided more info in my first question, I was just looking for a nudge in the right direction (the number needed and starting number will be changing via a database pull at some point):
int startingMask = 76;
int numberNumberNeeded = 10;
List<string> masks = new List<string>();
while (numberNumberNeeded > 0)
{
string newMask = (startingMask++).ToString().PadLeft(3, '0');
masks.Add(newMask);
numberNumberNeeded--;
}
string mask2 = (++startingMask).ToString().PadLeft(3, '0');
Postfix ++ startingMask++ first take value and then increment value
Sufix ++ ++startingMask first increment value and then take value

object.ReferenceEquals behave differently [duplicate]

This question already has answers here:
Two different "strings" are the same object instance?
(3 answers)
Closed 6 years ago.
string x = "alok b";
string y = "alok b";
string z = "alok";
//y += x.Replace(y, string.Empty);
z += " b";
Console.WriteLine(object.ReferenceEquals(x,y));
Console.WriteLine(object.ReferenceEquals(y, z));
How is first line is printing true and second false?
and changing to below statement is printing true.
Console.WriteLine(object.ReferenceEquals(y,string.Intern(z)));
It's called string interning.
When you create a string it creates an object (x).
when you create y, you just point to it"again", it points to the same one (has it).
When you create Z, and the do the += it creates a NEW one altogether, hence, it won't match the address in the memory for the previous one.

How to get the round off value of a decimal in C# [duplicate]

This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 8 years ago.
I want to get the round off value of a decimal number Suppose I am getting 24.86 than i want to get 25 as final value
Look at Math.Round(decimal) and the overload which accepts a MidpointRounding argument.
Simply
Math.Round(24.86)
This will round you value to 25.
Your own logic will be
decimal d = 1.5m;
decimal r = d - Math.Truncate(d);
if (r > 0)
r = 1 - r;
decimal value = d + r;

How to display number to 2 decimal places in mvc3,C#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using String Format to show decimal upto 2 places or simple integer
How to set decimal point in 2 decimal places?
I have Price field in my view. it have the following values 2.5 and 44.
I want to display this value to 2.50 and 44.00 i use the following code
#{decimal prolistprice = decimal.Parse(item.OtherFields["Price"].ToString());}
$#Math.Round(prolistprice, 2, MidpointRounding.AwayFromZero)
in which item.OtherFields["price"] is a object i convert it to string and then decimal
but Math.round is not working it shows 2.5 and 44 only..
Can anyone help this
Math.Round does just that - round.
To format the number you may use .ToString(formatString) like so:
item.OtherFields["price"].ToString("0.00")
Use string format function
1. string.Format("{0:n2}", 200000000.8776);
2. string.Format("{0:n3}", 200000000.8776);
3. string.Format("{0:n2}", 0.3);
/* OUTOUT
1. 200,000,000.88
2. 200,000,000.878
3. 0.30
*/
This should work for you
yourvalue.ToString ("0.00");
decimal dValue = 2.5;
string sDisplayValue = dValue.ToString("0.00");

Categories