-5

What is the best way to round double to two decimal places?

For example, I want to convert

3.45345323423423E+28

to

3.45
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
user2691432
  • 93
  • 1
  • 2
  • 10
  • I have tried Math.Round(3.45345323423423E+28,2)..but its not working – user2691432 Dec 31 '13 at 09:42
  • Convert it in output, formate it with the `.ToString(string);` function. – Jite Dec 31 '13 at 09:42
  • 2
    Erm, are you use that you want to convert `3.45345323423423E+28` to `3.45`? There are quite some numbers in between and the difference between those decimals is huge :-) – Darin Dimitrov Dec 31 '13 at 09:42
  • I have done :- double doubl = Math.Round(3.45345323423423E+28,2); doubl.ToString("0.00") – user2691432 Dec 31 '13 at 09:43
  • As far as I'm aware, 3.45345323423423E+28 is actually 34534532342342300000000000000, so... you can't display that as 3.45, because it's... slightly larger than 3.45... – Pete Ebdon Dec 31 '13 at 09:51

5 Answers5

7

You are aware that 3.45345323423423E+28 is a pretty large number and does not contain a decimal part?

(Stripping the scientific notation, your number is 34,534,532,342,342,300,000,000,000,000 which is, of course, an integer).

If you want to round a number to 2 decimal places then use Math.Round(value, 2); But do be aware that your example number will not be changed.

If however, you want to round to 2 significant figures, then see this answer: Round a double to x significant figures

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

use Math.round

Math.Round (value, 2);

Rounds a number to a specified number of fractional digits double-precision floating-point value.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
1

Use this:

Math.Round(value, 2);

This will round up your value to 2 decimal places.

Store it in some variable or display it.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Ashish
  • 1,856
  • 18
  • 30
  • It is giving me same output as 3.45345323423423E+28 – user2691432 Dec 31 '13 at 09:46
  • If going out of way add `0.005` to number and convert to string via `variable.toString()` later take substring of 4 places from left and convert back to int by `parseInt` this will give you rounded value – Ashish Dec 31 '13 at 09:52
1

First of all you should consider using some other data type for such large number. Perhaps decimal. Because precision of double is only up to 15-16 digits.

If you still persist with double then you can use Math.Round here.

double db = 3.45345323423423E+28;
db = Math.Round(db, 2);

However, if you change your mind to switch to decimal then following should do the trick.

decimal d = 3.45345323423423E+28m;
d = decimal.Round(d, 2);
Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

I think you want to display it like this: 3.45E+28

Then you should do something like this:

double number = 3.45345323423423E+28;
string result = number.ToString("0.00E+00");

Hope this will help you in your quest.

Casperah
  • 4,504
  • 1
  • 19
  • 13