Leave Only Two Decimal Places After the Dot

Leave only two decimal places after the dot

string.Format is your friend.

String.Format("{0:0.00}", 123.4567);      // "123.46"

Leave only two decimal places after the dot

string.Format is your friend.

String.Format("{0:0.00}", 123.4567);      // "123.46"

Leave only two places past the decimal point C#

You need to split the string then round the value. String.Format ($) will also do that in a single step:

string test = "0.1542 Mol";
string[] testParts = test.Split(' ');
string rounded = $"{Convert.ToDecimal(testParts[0]):0.00} {testParts[1]}";

Fiddle: https://dotnetfiddle.net/EQsJGf

Need only 2 digits after decimal point

Very simple. Try this

public double UptoTwoDecimalPoints(double num)
{
var totalCost = Convert.ToDouble(String.Format("{0:0.00}", num));
return totalCost;
}

How to display two digits after decimal point in SQL Server

select cast(your_float_column as decimal(10,2))
from your_table

decimal(10,2) means you can have a decimal number with a maximal total precision of 10 digits. 2 of them after the decimal point and 8 before.

The biggest possible number would be 99999999.99

Show only two digit after decimal

Use DecimalFormat.

DecimalFormat is a concrete subclass of NumberFormat that formats
decimal numbers. It has a variety of features designed to make it
possible to parse and format numbers in any locale, including support
for Western, Arabic, and Indic digits. It also supports different
kinds of numbers, including integers (123), fixed-point numbers
(123.4), scientific notation (1.23E4), percentages (12%), and currency
amounts ($123). All of these can be localized.

Code snippet -

double i2=i/60000;
tv.setText(new DecimalFormat("##.##").format(i2));

Output -

5.81

How to display values only upto 2 decimal places

Well, I tried it and got the correct result.

Below is the code that I used:

funding.amount= Math.Round(decimal.Parse(dr["Amount"].ToString()), 2).ToString();

//since the amount was of string type, therefore I used the above code. we can also use the below code:

decimal.Round(yourValue, 2, MidpointRounding.AwayFromZero);

http://msdn.microsoft.com/en-us/library/9s0xa85y.aspx

Show a number to two decimal places

You can use number_format():

return number_format((float)$number, 2, '.', '');

Example:

$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00

This function returns a string.



Related Topics



Leave a reply



Submit