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

  • SEARCH
  • Home
  • 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 8140119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:03:24+00:00 2026-06-06T12:03:24+00:00

I am using the following code to populate a Spinner in one of my

  • 0

I am using the following code to populate a Spinner in one of my Activities…

    for( double i = 0; i < 10 ; i+=0.1 ) {
        rVoltsList.add( Double.toString( i ) );
    }
    Spinner rVoltsSpinner = (Spinner) findViewById( R.id.recloseVoltsSpinner );
    ArrayAdapter<String> rVoltsAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, rVoltsList );
    rVoltsSpinner.setAdapter( rVoltsAdapter );

I was assuming this would give me a list as follows : 0.0, 0.1, 0.2, 0.3, 0.4, and so on.
However, this is what the list looks like when I run my program:

0.0
0.1
0.2
0.30000000000000000000000004
0.4
0.5
0.6
0.7
0.79999999999999999999
0.89999999999999999999
0.99999999999999999999
1.09999999999999999999
1.2
1.3
and this goes on until 9.99999999999999999998

any ideas?

  • 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-06-06T12:03:25+00:00Added an answer on June 6, 2026 at 12:03 pm

    Use this:

    rVoltsList.add( String.format("%.1f", i) );
    

    The problem is that Double.toString(i) will not round.

    The strange values are due to the fact that 0.1 (base 10) does not have an exact representation as a double in Java, so every time you add it to the loop variable, you are adding something a bit different than what you think (if you’ll pardon the pun).

    The same issue of rounding suggests that you should not be using a double as a loop variable. You are very unlikely to exactly hit your loop limit exactly. I would rewrite your loop as follows:

    for( int i = 0; i < 100 ; ++i ) {
        rVoltsList.add( String.format("%.1f", i / 10.0) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using currently the following code to populate a combobox: combobox.DataSource = datatable;
I'm using the following code to populate listview Oncreate method: SimpleAdapter adapter = new
I am using the following code to populate an array: $number = count ($quantitys);
I'm using the following Javascript code to populate a DIV with an ordered list:
Following is the code to populate combobox using DataSource Dim CountryList As Array =
i m using following code to populate dropdownlist dynamically... i want that value should
I am using following code to populate DropDown: $(document).ready(function(){ $('#inDistrict').sSelect(); $(#inDistrict).change(function(){ $.getJSON(filldistricts.php,{id: $(this).val(), ajax:
I'm using the following code to populate a UITableViewController - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
I'm using the following code to populate a DataGridView (sqliteAdapter derives from DbDataAdapter): sqliteAdapter.SelectCommand.CommandText
I'm trying to pre-populate a wxPython DatePicker with a value using the following code:

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.