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);
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 6 years ago.
Improve this question
I have two dates.
ReleaseDates = "11/2/2016";
LiveDate = "11/02/2016";
In the above two dates are same. But in my below coding it returns FALSE.
if (ReleaseDates.Contains(LiveDate.TrimStart(new Char[] { '0' })))
{
}
Your code does not work because TrimStart removes characters at the start of the string. It looks like LiveDate has no zeros at the beginning; the character '0' that you want to trim is at index 3, preceded by other characters. That is why it is not getting trimmed.
Comparing strings representing dates is an error-prone strategy. You would be better off parsing both strings, and comparing the results as DateTime objects.
In general, you should keep dates as objects designed for date representation - for example, DateTime or DateTimeOffset. This would let you customize date representation for display purposes, and avoid errors when date format changes from mm/dd to dd/mm.
This is because you are not comparing dates, you are comparing two strings that are not the same. Your best shot here is parsing first and then comparing.
DateTime releaseDate = DateTime.Parse(ReleaseDates);
DateTime liveDate = DateTime.Parse(LiveDate);
if (releaseDate == liveDate) // This should be true.
{
// Do stuff.
}
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 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.
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 am currently trying to get a label(lets name it lblMessage) to pull information based on what the user has picked threw radio buttons and check boxes. Lets make the theme a sundae maker form.. people can select there flavor, size, and addons.
I'm just trying to make it so the label displays once they click the confirm button, a message that identifies the number of sundaes, sundae flavor and size the person ordered. like.. (You ordered 2 small hot fudge sundaes which will cost you
$x.xx (where x.xx is the cost for all sundaes ordered). I tried looking this up but i couldn't seem to find what to put into the label code... =\
I am a beginner at C# ... If any other information is needed, I can provide.
What you want to do is set the text of the label. You can use the String.Format method to take a string and replace certain values with values from variables, for example:
var result = MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
Decimal total = int.Parse(textBox1.Text) * 1.25m;
// The {0} will be replaced with the first argument after the format string.
// The total.ToString("C") tells the decimal to format the string into a currency string (http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=vs.110).aspx)
label1.Text = String.Format("You ordered {0} small hot fudge sundaes, which will cost you {1}", textBox1.Text, total.ToString("C"));
}
Now this example still fails if the textbox does not contain an int, but that can easily be handled using the TryParse method.
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