Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:05:26+00:00 2026-05-26T16:05:26+00:00

I am developing an app and want to round off values i.e if the

  • 0

I am developing an app and want to round off values
i.e if the output is 4.8 I want to display 4.8
while if the output is 4.0 , I want to display 4

Also, it would be great if I could precisely round values : as in if value is 4.34 then round to 4.3 while if its 4.37 then round it to 4.4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:05:27+00:00Added an answer on May 26, 2026 at 4:05 pm

    One way to round floating point values is to just add 0.5 and then truncate the value.

    double valueToRound = GetTheValueFromSomewhere();
    double roundedValue = (double)((int)(valueToRound + 0.5));
    

    This will round 1.4 down to 1.0 and 1.5 up to 2.0 for example. To round to other decimal places as you mentioned, simply multiply the initial value by 10, or 100, etc. use the same sort of code, and then divide the result by the same number and you’ll get the same result at whatever decimal place you want.

    Here’s an example for rounding at an arbitrary precision.

    double valueToRound = GetTheValueFromSomewhere();
    int decimalPrecisionAtWhichToRound = 0;
    double scale = 10^decimalPrecisionAtWhichToRound;
    double tmp = valueToRound * scale;
    tmp = (double)((int)(tmp + 0.5));
    double roundedValue = tmp / scale;
    

    So, if decimalPrecisionAtWhichToRound is set to 0 as in the above it’ll round to the nearest whole integer. 1.4 will round to 1.0. 1.5 will round to 2.0.

    If you set decimalPrecisionAtWhichToRound to 1, it would round to the nearest tenth. 1.45 would round to 1.5 and 1.43 would round to 1.4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a web-app where I want to provide a download link to
I'm developing a Mac OS X Cocoa app and want to use several .dylib
I have a command line Ruby app I'm developing and I want to allow
I'm developing an ASP.NET 2.0 app using Visual Studio 2008. If I want to
I have a winforms (VB 2008) based app that I'm developing and I want
While developing my app I have come to realize that the majority of my
i am developing app in Blackberry which have GPS Functionality. so first i want
Im newbiew to Android developmt. I'm currently developing an app and I want either
I'm developing an Android app and I want to search for the first result
I'm developing JSP/Servlets App,and I want to execute a service at a specific time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.