Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I have decimal part of a decimal division e,g.
decimal d = 10;
decimal result = d/10;
This gives 1, how can I have 1.0 (still as a decimal, not string)?
The decimal value of 1 and 1.0 are equal. I'm assuming you want to see the format of the number with the decimal place.
To do that, you can use .NET format strings. Personally, I generally use custom format strings, so it would be:
string formattedDecimal = result.ToString("#,##0.0");
The # character means to put a digit there if one exists, but don't use leading zeroes. The 0 character means you're guaranteed to have a digit there, even if it's zero, so 1 will be formatted as 1.0. If you don't want the grouping, you can leave out the hashes and comma and just have ToString("0.0"), which will give you the same thing, leaving out any potential thousands grouping.
If it's a representation issue:
using System;
public class Test
{
public static void Main()
{
decimal d = 10;
decimal result = d / 10;
Console.WriteLine( string.Format("{0:0.0}", result ) );
// or
Console.WriteLine( result.ToString("0.0") );
}
}
They all print 1.0 as output.
DEMO
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.
For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.
Is there a way to prevent this?
If you want to display your Decimal with two decimal places, please use :
myDecimal.ToString("N2");
You may want to take a look at Standard Numeric Format Strings for more information.
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);
Please find this Post
I'm not sure about if you mean this, but you can try the toString() method in currency format this way:
double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"
I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to get 16 bits from Listbox and Not those bits. but I get wrong answer can anybody help me what's going wrong?
Input = 1101010101110101
Output = -1101010101110102
the expected result is : 0010101010001010
here is my code.
public static long NotInputEqualWhichBit(String str)
{
long Input = (Int64.Parse(str));
long notInput = ~Input;
return notInput;
}
and i called this method bellow
string listBoxLine = ListBox_content.Items[2].ToString();
long res2 = checkLogic_Class.NotInputEqualWhichBit(listBoxLine);
MessageBox.Show(res2 +"");
Your question is a little vague, so my answer will be too.
You are parsing a binary string as an int64. Basically, you're converting a 11 (string, but to you it is the binary value of three) into an 11 (decimal value of eleven).
If your question is about how a "2" digit can show up in your result, then this is your answer: when you convert your input to a int64, the conversion assumes that you're using decimal notation, not binary. Therefore, it interprets a different mathematical value.
Decimal notations allow for more than just 0 and 1, so that's why the 2 shows up.
If that's not your question, continue reading.
In C# (among other languages), the first bit that you find in an integer is called the "signed" bit. If it is 0, your integer is positive. If it is 1, your integer is negative.
Because you are inverting every bit, you are also flipping the signed bit; therefore turning your value into a negative number.
Change the following line:
long Input = (Int64.Parse(str));
Into the following:
ulong Input = (Convert.ToUInt64(str));
This is an unsigned long. Simply put, it does not have a signed bit and therefore cannot turn into a negative number.
I think the most important issue is that you want to make certain you have 16 bit and invert these. That is done by typecasting to ushort.
ushort i = 5;
Console.WriteLine((ushort)~i);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am attempting to write out to a fixed format text file and am having some trouble formatting the numbers properly. It needs to be padded with leading zeros and an implied decimal.
Example:
43.80
The output would need to be:
0000004380
So far, I have attempted to convert the double to a string, replace the decimal, and then pad with "0".
((amount.ToString()).Replace(".","")).PadLeft(10, '0')
The problem with this is when the number ends with zeros. The above example comes out as:
0000000438
Any suggestions?
decimal value = 34.80M;
int intVal = (int)(value * 100);
return intVal.ToString("0000000000");
You could try:
((string.Format("{0:0.00}", amount)).Replace(".","")).PadLeft(10, '0')
That way you don't lose the 2nd decimal place when the value is 0.
You can pass a .ToString() format string in as described in this article:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Does the incoming value always have two decimal places? If so, you could multiply by 100 to get rid of the decimal, and then to .ToString("D10")
(Int)(43.80 * 100).ToString("D10")
This should do the job : $"{(int)(amount * 100):D010}"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hello guy i am creating calculator and i want to convert these values to float
string num = "2+6+8+9";
so they will automatically add and give total of 25?
is there anyway to do this?
sorry for my bad english
Use DataTable.Compute ;-)
DataTable table = new DataTable();
int i = (int) table.Compute("2+6+8+9", null); // 25
For all rules and which operators are supported: DataColumn.Expression
If you don't know the type of the result use a floating point type always and System.Convert.ToDouble which accepts everything that's IConvertible, for example:
double d1 = System.Convert.ToDouble(table.Compute("2+6+8+9", null)); // 25.0
double d2 = System.Convert.ToDouble(table.Compute("2+6+8+9/2", null)); // 20.5
You are looking for an expression evaluator. There are many available. For example: http://csharpeval.codeplex.com/
The question asked in your subject is straight forward. You can convert a string to a float like this:
double f = double.Parse("2.5");
However, if you want to handle arbitrary expressions as described in your question, that requires a bit more work. Fortunately, others have done this work. I wrote my own expression evaluator and you can see it in my article A C# Expression Evaluator. It supports variables and functions in addition to supporting the sample expression in your question.
This may get you what you want:
string[] inputArray = Regex.Split(num, #"\D+");
float results = 0;
foreach (var item in inputArray )
{
results += float.Parse(item);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i wanna ask. How do i put "," sign on the number whenever the number length is more than 3, then add 1 "," sign.
for example:
I have a number "100000000", and i want computer display it as "100.000.000,00", how do i do that?
Here is the image:
In picture above, shown that the SubTotal is "10000", i want computer display it as "10.000,00" and the Total beside SubTotal is "10000000", i want computer display it as "10.000.000,00".
My question is: how do i do that?
Thanks
A lot of it depends on the control(s) you are using. If you're using plain text boxes you can just set the format when setting the Text value:
txtbox1.Text = total.ToString("N2"); // numeric with separators and 2 decimal places
Other third-party controls let you choose the format with a property such as NumberFormat. Grid controls usually set the format on a column rather than an individual cell.
You should use
amount.ToString("N");
If you're doing it programmatically:
int myNumber = 10000000;
string output = String.Format("{0:n2}", myNumber);
or
int myNumber = 10000000;
string output = myNumber.ToString("n2");
The number after n is the number of decimal places (which can be 0 if you want).
Or you might need to set a format string of a user control to "n2" (without quotes) depending on how you are displaying the numbers.
Have a look at standard numeric format strings and custom numeric format strings on MSDN.
You should use the numeric format specifier to achieve what you want:
number.ToString("N", CultureInfo.InvariantCulture);