CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Location
    Singapore
    Posts
    40

    converting 'values' to decimal with 0.0

    how do you convert any value int, double, float and so on into a decimal with only .0 decimal place

    example

    height 155.55
    height2 213.5
    height3 444.0

    average height = 271.0166666666=(height+2+3)/3
    but i want to display only 271.0

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: converting 'values' to decimal with 0.0

    There is a Round method in the Decimal structure (but you would not find any equivalent method for double types) . It rounds the decimal variable to the specified number of decimal places. For examples and more detail see here. Hope this helps.

    Don't forget to rate good/helpful posts.

  3. #3
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: converting 'values' to decimal with 0.0

    other wise, use the string's format method to acquire the correct effect.
    Mike

  4. #4
    Join Date
    Jul 2005
    Location
    Singapore
    Posts
    40

    Re: converting 'values' to decimal with 0.0

    i see thanks to both

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: converting 'values' to decimal with 0.0

    You can display any number as a string with exactly 1 decimal place by calling ToString("n1"). This will display 1 decimal place even if the number is a round integer. It will also perform standard rounding on the second decimal place. It further has the advantage of affecting display only and not the actual numerical value of the variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured