Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to 2 decimal place

Tags:

c#

I have a string (confirm to be of decimal expression) 0.4351242134

I want to convert to a string with two decimal place 0.44

How should I do in C#?

like image 782
william007 Avatar asked Aug 22 '12 12:08

william007


People also ask

How do you convert a string to two decimals?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

How do you convert a string to two decimal places in Java?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do you float up to 2 decimal places?

In short, the %. 2f syntax tells Java to return your variable ( val ) with 2 decimal places ( . 2 ) in decimal representation of a floating-point number ( f ) from the start of the format specifier ( % ).


1 Answers

Well I would do:

var d = "0.4351242134";
Console.WriteLine(decimal.Parse(d).ToString("N2"));
like image 77
Renatas M. Avatar answered Oct 29 '22 11:10

Renatas M.