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 3406824
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:40:32+00:00 2026-05-18T05:40:32+00:00

So I’m trying some code out to convert numbers into strings. However, I noticed

  • 0

So I’m trying some code out to convert numbers into strings. However, I noticed that in certain cases it does not preserve the last two decimal places. For instance I type 1.01 and 1.04 for addition and I get back 2.04. If I type just 1.05 it preserves the number and returns it exactly. I get whats going on things are being rounded. I don’t know how to prevent it from being rounded though. Should I just consider sending (1.01+1.04) to self as only one input?

Warning! I haven’t tried this yet so don’t know if its supported:

 user_input = (1.04+1.01) #entry from user
 user_input = gets.to_f
 user_input.to_test_string

What I have so far:

    class Float
     def to_test_string

      cents = self % 1
      dollars = self - cents
      cents = cents * 100

      text = "#{dollars.to_i.en.numwords} dollars and #{cents.to_i.en.numwords} cents"

      puts text
      text
     end
    end
  puts "Enter two great floating point numbers for adding"
  puts "First number"
  c = gets.to_f
  puts "Second number"
  d = gets.to_f
  e = c+d
  puts e.to_test_string
  puts "Enter a great floating number! Example 10.34"
  a = gets.to_f 
  puts a.to_test_string

Thanks for the help! Post some code up so I can try!

  • 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-18T05:40:32+00:00Added an answer on May 18, 2026 at 5:40 am

    First of all: never use float for money — Use Float or Decimal for Accounting Application Dollar Amount?

    irb> x = 1.01 + 1.04
    => 2.05
    irb> y = x % 1
    => 0.04999999999999982
    irb> (y * 100).to_i
    => 4
    

    But if want it VERYVERYVERY much:

    irb> (y * 100).round.to_i
    => 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.