How to display 100 from 99.99 from stringformat [duplicate] - c#

This question already has answers here:
C# Double - ToString() formatting with two decimal places but no rounding
(15 answers)
Closed 1 year ago.
I would like to have it round up to 100 using stringformat, not math.round because the way in the code I can't use math.round.

If you set your precision to a single digit like this, the number will be rounded up to 100.0. You can use string interpolation and not have to specify String.Format like this:
Console.WriteLine($"{99.99:0.0}");

If you want to round the number to the nearest integer, you can try:
var str = string.Format("{0:.}", 99.99); // Will return "100"
If you want to always have one digit after the delimiter, you can change it to:
var str = string.Format("{0:.0}", 99.99); // Will return "100.0"
You can always check the official documentation for the entire set of options:
String.Format Method

Related

Convert int and float to 3 decimal places [duplicate]

This question already has answers here:
Formatting a float to 2 decimal places
(9 answers)
C# How to format a double to one decimal place without rounding
(6 answers)
Closed 11 months ago.
This is what I want to do:
if a double field has more than 3 decimal places then it should convert to 3 decimal figures and if no decimal places are present then it should convert to 3 decimals
e.g 12.878999 -> 12.878
120 -> 120.000
I cannot use string.Format() as I want the double field to stay double.
The first example requires Math.Round, eg Math.Round(d,3,MidPointRounding.ToZero).
The second isn't meaningful. Trailing decimal zeroes aren't significant. In the real types (float, double) they don't affect the storage of the number. The call Math.Round(120d, 3, MidpointRounding.AwayFromZero) will print 120 without a format string.
Displaying a double with three trailing zeroes is a formatting operation.
Update
From the comments it appears the actual problem is how to format a report sum in DevExpress Reports. All report engines allow specifying a format for fields and cells.
The Format Data page in the DevExpress Reports docs shows how to modify the FormatString property for a specific value
You can use Math.Round to achieve it
var value = Math.Round(12.878999, 3, MidpointRounding.ToZero);
For the integer type, you can do it this way
var value = 129 + 0.000m; //extend 3 decimals for an integer number

How can i Convert an string to double but with power of 10 [duplicate]

This question already has answers here:
Convert from scientific notation string to float in C#
(2 answers)
Closed 1 year ago.
I'm stuck with a problem and I have to display these numbers correctly using the power of ten.
I know that the number in the end has to do with the fact that the decimal point is shifted the number to the right, only how I can do it or how I have to parse the number is a mystery to me.
I have the numbers as a string
The whole thing is written in C #
"1.11632e+007"
"1.30357e+008"
The result must look like this
The output can be as int or string does not matter
11163200
130357000
I have no idea how to do this. Can you help me?
the internal representation of a float is in binary, and there is nothing you can do about it.
If you want to print a float avoiding the scientific notation even for huge numbers it's something like
Console.WriteLine(x.ToString("F"));
Assuming you are trying to parse it, System.Globalization.NumberStyles.Float should help:
var parsed = decimal.Parse("1.11632e+007", System.Globalization.NumberStyles.Float);
Console.WriteLine(parsed); // outputs "11163200"

Display two decimal places for a string value [duplicate]

This question already has answers here:
Two Decimal places using c#
(13 answers)
Closed 6 years ago.
I've retrieved a decimal value from database as a string:
string get_amt = dt_.Rows[i]["billing_amt"].ToString();
Values of billing_amt are 100.0000, 150.0000, 0.0000.
I tried to display the value of get_amt rounding up to two decimal places using the code:
string.Format("{0:0.00}", get_amt)
but it is not working. How can I show the value of get_amt as 100.00 or 150.00? Please suggest.
You'll probably want to try something like:
double get_amt = 0;
if (dt_.Rows[i]["billing_amt"] != DBNull.Value)
get_amt = Convert.ToDouble(dt_.Rows[i]["billing_amt"]);
Then:
string.Format("{0:0.00}", get_amt)
should work.
It's currently not working as it's a string value your trying to format - which wont have decimal places.
Strictly speaking, you could use Regex to cut the string down to a 2 decimal point number like so:
string formatted = Regex.Match(unformattedString, #"^-?\d+\.\d{2}").Value;
That being said, you almost never want to use a string to hold number information. It's a bother and a pain to get them to do what you want. It's much more advisable to store them as either a double or a decimal, then convert to string only when you have to.

Generating a number with 7 digits with the ability to have zeros at the front [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# convert int to string with padding zeros?
I am trying to generate a random number however in the ranges I have included "0000000, 9999999" I want it to show these zeros when a number is generated for example the following outputs would be:
"00037544"
"01659369"
"75484957"
"00000001"
I am using the:
int num = rnd.Next(0000000, 9999999);
The problem with that is sometimes it will generate:
"1648"
"947596"
"18500" etc
Is there something that im missing?
Thanks for looking
You have to convert it to the string and then use PadLeft.
string number = num.ToString().PadLeft(7, '0');
Or Simply
string number = num.ToString("0000000");
int just represents a number; it doesn't know how many zeroes it has.
When you display that number, you can give it as many leading zeroes as you want.
For example, num.ToString("D7") will create a string with enough leading zeroes to make it 7 digits long, as will String.Format("{0:D7}", num). Console.WriteLine("{0:D7}", num) works the same way.
Take a look at the docs.
You're generating integers, and you need to format them. something like num.ToString("0000000")should suffice.
Any number with zeros to the left are logically identical to the same number without these zeros. I think what you want is a way to print the zeros on screen.
Example: 5 is equal to 00005; but you want to print the number 5 with 5 digits.
The way you would do this is language dependent.
try this:
Random rnd = new Random();
int num = rnd.Next(0000000, 9999999);
string num2 = num.ToString("D7");

how to convert decimal points? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
i have value of 1.564 and 1.565 i need round this value to 1.56 and 1.56,which function suitable for this in c#.
do a multiply by 100 followed by a floor and followed by a divide by hundred. I am sure that there is a better way of doing it though
Math.floor(n*100)/100
To remove the less significant digits (1.348 -> 1.34):
Math.Floor(number * 100) / 100;
To round the number to two decimals:
Math.Round(number, 2);
To represent it as a string, for display:
number.ToString("#.00");

Categories

Resources