C# format a double with ToString, and dispay zero value - c#

I'm trying to find the best way to display a double in C# as follows:
7.345 should display as "73"
100.0 should display as "100"
0.234 shoud display as "02"
The input is a value between 0.00 and 10.00. I need to convert it to a filename. E.g. in case of a value of 5.4234, I should display "img54.jpg".
The problem is that I can't figure out how to display zero values in ToString() of doubles.
I tried this:
(10 * 7.345).ToString("##.") => correct
(10 * 10.00).ToString("##.") => correct
(10 * 0.000).ToString("##.") => FAIL, doesn't display anything.
(10 * 0.000).ToString("D2") => FAIL, D is not allowed in doubles
I can of course do some sophisticated string building, but if it's possible to use ToString, that would be much better of course.
Anyone an idea?

What do you want 0.00 to display as? "00"?
In that case you can try with format ToString("00.") instead.

Can you simply check if the double is 0? and If it is, set img00.jpg to your filename. That seems a lot easier than reworking your algorithm.

Why don't you convert it to an int?
int result = (int)(input * 10.00);
return result.ToString();

You could just convert to int before formatting like this:
((int)(10 * 7.345)).ToString("D2")

If you always have the input number of this format: #.###
You can multiply it by 1000 and divided by 100 and cast the result to an integer.
7.345 * 1000 = 7345 / 100 = 73.45 => Convert.ToInt32 => 73
0.000 * 1000 = 0 / 100 = 0 => Convert.ToInt32 = 0
Or you can multiply by 10 and convert to Integer.

return ((int)(input * 10.00)).ToString().SubString(0, 2);

double val = 7.345;
string result = val.ToString("0.#").Replace(".","");

Related

c# large fractional decimal split

My Requirement is Fractional Amount FIrst 2 decimal part add customer bank account and others fractional part add in dispute wallet account .
var amount = 40.235667745465465
I want to convert it 2 different variable
var customerBalance = // ??? - should be 40.23
var disputeBalance = amount - customerBalance
How can I do calculate the step marked ??? ?
This will work fine.
var firstAmount = Math.Floor(amount / 0.01) / 100 ;
var secondVariable = amount - firstAmount;
You probably want:
var firstAmount = Math.Round(amount, 2);
but note that this can round up as well as down; you may want to check whether secondVariable comes out negative, and if so: compensate.
Another way to look at it is to multiply by 100 and take the integer/decimal parts (hint: Math.Floor), then divide by 100 again.
If you are looking for a string, then it can be:
string secondVariableAsString = string.Format("{0:0.00}", secondVariable);
Another is:
Math.Truncate(100 * secondVariable) / 100;
However, this will cause overflow for large numbers.

Asp.net C# variable & value calculation

double a =(80/100);
lbl1.Text = a.ToString();
answer is display as 0 why ?
how to get correct answer?
the expression (80/100) evaluates to 0 because 80 and 100 are both interpreted as int, and int divided by int always results in an int. What you need is this:
double a =(80d/100d);
lbl1.Text = a.ToString();
That way the expression uses double values and you get what you expect - 0.8.

Round off in listview

I am trying to round off each row to the nearest 1.0 in a column of a listview, so meaning 1.58 should show 2.00 and 1.48 should be 1.00 -
Math.Round(listView1.Columns[2].ToString(), 10);
You need to use 0 in digits parameter. You expect no digits here, but you're passing 10 to digit parameter which says to round with 10 digits after decimal.
var res = Math.Round(1.58, 0);//2
var res = Math.Round(1.48, 0);//1
Just saw you try to Round the string, You'll have to convert it to Double or decimal or whatever.
var rounded = Math.Round(Double.Parse(listView1.Columns[2].ToString()), 0);
In case you want to get String as a final result (as far as you've put ToString() in your code), you may just use appropriate formatting string ("F0" in your case):
String result = (1.58).ToString("F0"); // <- "2"
...
String result = (1.48).ToString("F0"); // <- "1"
You can't round a string directly.
string val=listView1.Columns[2].ToString();
double i;
if(Double.TryParse(val, out i))
{
Console.WriteLine(Math.Round(i)); // you can use Math.Round without second
// argument if you need rounding to the
// nearest unit
}

TimeSpan.TotalMinutes without seconds

I am using the code
var minutesPassed = (DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;
to calculate how much minutes passed between two dates. The result which I get looks like
254.54445556
I get minutes and seconds. How to get result which would contain only minutes like this
254
?
Just explicitly convert the result to int:
var minutesPassed = (int)(DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;
Use Math.Floor() to convert 254.xxxx to 254:
var minutesPassed = Math.Floor((DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes);
You can just get the int part
int minutes = (int) (DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;
this will get you the int part of the value.
EDIT: as far as rounding of value is considered. That is not true. Consider the following:
double d = 254.99999999999d;
int test = (int)d;
Here test will hold 254, not 255
The only problem with the explicit cast is OverFlowException

MMMMMMSS what kind of a time duration format is this?

i have a file which states duration for a particular event in format MMMMMMSS.Does any one know what kind of format is this for time duration and how to convert it into seconds.I'm using C# language
If the format is really M...MSS (supplied as an integer value), converting it to seconds is quite easy:
var seconds = (value / 100) * 60 + (value % 100);
Why does it work?
value / 100 removes the last two digits (integer division), thus returning MMMMMM, and
value % 100 returns the last two digits (modulo), i.e., SS.
The remainder of the formula is MMMMMM * 60 + SS, which should be pretty self-explanatory.
My guess based on the format is that it could hold a maximum value of 99999959, which would mean 999999 minutes and 59 seconds. But that is pure conjecture, and some sample data would help to bolster this idea. You may never know for certain though.
You should at least be able to determine whether the SS part ever exceeds 59 or not, which would be very important to know.
var input = "02345612";
int minutes = int.Parse(input.Substring(0, 6));
int seconds = int.Parse(input.Substring(6, 2));
int totalSeconds = minutes * 60 + seconds;

Categories