How can I show the decimal and two places after it when converting an int to double?

B. Clay Shannon

Given values such as 123, 1234, and 12345, I need these to be converted into, respectively, 1.23, 12.34, and 123.45

This code, when I enter 123, then 1234, then 12345 into textbox2:

int originalVal = Convert.ToInt16(textBox2.Text);
double doubled = Convert.ToDouble(originalVal/100);

...gives me 1, 12, and 123 instead of the expected 1.23, 12.34, and 123.45.

What do I need to do to get the desired result? Please note that this is a Windows CE / Compact Framework project using VS2003 and .NET 1.1.

Greg Hewgill

You are doing integer division, because both originalVal and 100 are themselves integers. Try:

originalVal/100.0

The use of 100.0 makes the compiler generate code for a floating point division. Alternately, this would have the same effect:

Convert.ToDouble(originalVal)/100

where you are converting your originalVal integer to a double before doing the division.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to format a double with fixed number of significant digits, regardless of the decimal places?

분류에서Dev

How can I stop printing a float 3 spaces after decimal?

분류에서Dev

In MS Excel how do I set the number of decimal places dynamically?

분류에서Dev

defining decimal places of a double number in Java

분류에서Dev

How can I call jQuery after show extjs window?

분류에서Dev

Why does this decimal show 8 decimal places on ToString()?

분류에서Dev

Using JavaScript, how do I use the .toExponential() function, but only using two decimal places (9.99e9, instead of 9.9999e9)

분류에서Dev

Converting a double pointer to an int pointer

분류에서Dev

Why would a c++ double be limiting itself to 5 decimal places?

분류에서Dev

How can I show mActionBar when it's invisible, and hide it when it's visible?

분류에서Dev

Excel Graph: how can I show two values in the same bar? Not using stacked?

분류에서Dev

How to convert seconds in Double to two int's for seconds and milliseconds?

분류에서Dev

How to convert seconds in Double to two int's for seconds and milliseconds?

분류에서Dev

How can I remove the decimal parts from numbers in Kotlin?

분류에서Dev

How can I remove last digit from decimal number in PHP

분류에서Dev

How can I show that an email is authentic?

분류에서Dev

How can I hide or show the inner level

분류에서Dev

How can I write a method that return an int from an array of buttons when one is pressed?

분류에서Dev

How can I prevent ColdFusion from converting HTML Entity?

분류에서Dev

How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

분류에서Dev

Round number to decimal places

분류에서Dev

result decimal places javascript

분류에서Dev

If I have apply(i: Int), how can I have map?

분류에서Dev

How can I "diff" two files with Nautilus?

분류에서Dev

How can I return two values in a controller

분류에서Dev

How can I diff two XML files?

분류에서Dev

How can I Join Two Query

분류에서Dev

How can I combine these two statements?

분류에서Dev

How can I combine two files on Windows?

Related 관련 기사

  1. 1

    How to format a double with fixed number of significant digits, regardless of the decimal places?

  2. 2

    How can I stop printing a float 3 spaces after decimal?

  3. 3

    In MS Excel how do I set the number of decimal places dynamically?

  4. 4

    defining decimal places of a double number in Java

  5. 5

    How can I call jQuery after show extjs window?

  6. 6

    Why does this decimal show 8 decimal places on ToString()?

  7. 7

    Using JavaScript, how do I use the .toExponential() function, but only using two decimal places (9.99e9, instead of 9.9999e9)

  8. 8

    Converting a double pointer to an int pointer

  9. 9

    Why would a c++ double be limiting itself to 5 decimal places?

  10. 10

    How can I show mActionBar when it's invisible, and hide it when it's visible?

  11. 11

    Excel Graph: how can I show two values in the same bar? Not using stacked?

  12. 12

    How to convert seconds in Double to two int's for seconds and milliseconds?

  13. 13

    How to convert seconds in Double to two int's for seconds and milliseconds?

  14. 14

    How can I remove the decimal parts from numbers in Kotlin?

  15. 15

    How can I remove last digit from decimal number in PHP

  16. 16

    How can I show that an email is authentic?

  17. 17

    How can I hide or show the inner level

  18. 18

    How can I write a method that return an int from an array of buttons when one is pressed?

  19. 19

    How can I prevent ColdFusion from converting HTML Entity?

  20. 20

    How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

  21. 21

    Round number to decimal places

  22. 22

    result decimal places javascript

  23. 23

    If I have apply(i: Int), how can I have map?

  24. 24

    How can I "diff" two files with Nautilus?

  25. 25

    How can I return two values in a controller

  26. 26

    How can I diff two XML files?

  27. 27

    How can I Join Two Query

  28. 28

    How can I combine these two statements?

  29. 29

    How can I combine two files on Windows?

뜨겁다태그

보관