How to show Zeros when a decimal is converted to string? [duplicate] - c#

This question already has answers here:
Why does 0.ToString("#.##") return an empty string instead of 0.00 or at least 0?
(5 answers)
Closed 8 years ago.
I have several decimal numbers that I need to show as strings (no need for decimal places). I use the following code:
mytextblock.text= mydecimalnumber.ToString("#");
However this code will show nothing if the number is 0. I need to display a "0" instead of an empty string. I can do this using if and else but I don't think this is the best solution to do with many decimal variables.

Some user "David.." posted this answer and it worked for me but then he deleted it later:
He mentioned that ToString("#") will only show digits that are not zeros.
In order to default to 0, I should use ToString("0")

Related

Round off decimal value in Nearest 10 or Zero [duplicate]

This question already has answers here:
C# - Math.Round
(5 answers)
Closed 4 years ago.
Following values i would like to convert to round off figure. likes:
60.72 --> 60.70
170.76 --> 170.80
Currently, I'm converted to round off value using below method:
getFee.ServiceRequestFee.ToString("N")
I'm not sure which Match.Round method suitable to my requirement.
Edit:
protected string Getroundoffdecimalvalue(string servicerequestsfee_val)
{
servicerequestsfee_val = Math.Round(Convert.ToDecimal(servicerequestsfee_val), 2).ToString();
return servicerequestsfee_val;
}
I used this function even after i return 60.72 only and my expectation should 60.70.
Math.Round(yourNumber, 1)
The second parameter is number of decimal places to round to. In your case you want 1 decimal place as an end result.
**
You need to overloadMath.round that takes the decimals parameter of
your choice and convenience.
**
Use Math.round and if needed convert the same to string.
Math.Round(var_name,2)

Force two decimal places in C# [duplicate]

This question already has answers here:
Leave only two decimal places after the dot
(13 answers)
Closed 8 years ago.
I've this problem and can't find a solution.
This is super easy and i don't know why can't i find a solution.
Problem:
if a value returns for example "16.60", in c# i'll read "16.6", but i need 0 as well, because of paypal API, wich only accepts a value with no decimal numbers, or if it has to have decimal numbers the minimum and maximum must be 2.
So how can i make this?
i've tried this:
string value_f = "16,6";
decimal value_f_d = decimal.Parse(value_f);
value_f_d = (decimal)Math.Round(value_f_d, 2);
value_f = value_f_d.ToString("#.##");
value_f = value_f.Replace(',', '.');
i want this output: 16.60, but gives this: 16.6
string output = value_f_d.ToString("#.00", CultureInfo.InvariantCulture);
(using System.Globalization in your using declarations at the top)

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

for loop formatting mvc3 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
I have an int that is passed through to the a view and it need to formatting so it is not one big block of number. e.g
0
00
000
000,0
000,000
000,000,000
How can i do this using a for loop?
thanks
You may want to output the integer as i.ToString("###,###,###");
However, this would not display a value if the int is 0.
You could try using System.Drawing.Color to represent the colour value rather than an int. This has a method ToArgb()

Trimming a float [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Leave only two decimal places after the dot
Formatting a float to 2 decimal places
If I have a float that consists of something like 153.2154879, is there any way to convert it to string but only show 4 decimal places? I know I can format it using "000.000", but the front number doesnt always have to be 3 digits. So is there a way to show all the front numbers (153), but only the first 4 characters after the point in a string?
Something like this should do:
your_number.ToString("0.####");
This will show a max of 4 decimal places.
I usually use a format string like "#0.0000".
You can use the C# function Math.Round function.
float a= 153.213456;
Math.Round(a,3);
this would round up the number to 153.213
then get convert it to string.

Categories