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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:53:14+00:00 2026-06-08T19:53:14+00:00

Consider the following code that is intended to round numbers to the nearest one-hundredth

  • 0

Consider the following code that is intended to round numbers to the nearest one-hundredth and serialize the result to JSON:

require 'json'
def round_to_nearest( value, precision )
  (value/precision).round * precision
end
a = [1.391332, 0.689993, 4.84678]
puts a.map{ |n| round_to_nearest(n,0.01) }.to_json
#=> [1.3900000000000001,0.6900000000000001,4.8500000000000005]

Is there a way to use JSON to serialize all numbers with a specific level of precision?

a.map{ ... }.to_json( numeric_decimals:2 )
#=> [1.39,0.69,4.85]

This could be either with the Ruby built-in JSON library or another JSON gem.

Edit: As noted in comments to answers below, I’m looking for a general solution to all JSON serialization for arbitrary data that includes numbers, not specifically a flat array of numbers.


Note that the above problem can be fixed in this specific case by rewriting the method:

def round_to_nearest( value, precision )
  factor = 1/precision
  (value*factor).round.to_f / factor
end

…but this does not solve the general desire to force a precision level during serialization.

  • 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-08T19:53:15+00:00Added an answer on June 8, 2026 at 7:53 pm

    I’d just pre-round it using ruby’s built-in round method: http://www.ruby-doc.org/core-1.9.3/Float.html#method-i-round

    a.map{ |n| n.round(2) }.to_json
    

    That looks clean to me instead of getting all types of custom libraries and passing in arguments.

    Edit for comment:

    I know you can do that with activesupport.

    # If not using rails
    require 'active_support/json'
    
    class Float
      def as_json(options={})
        self.round(2)
      end
    end
    
    { 'key' => [ [ 3.2342 ], ['hello', 34.1232983] ] }.to_json
    # => {"key":[[3.23],["hello",34.12]]}
    

    More exact solution: better monkey-patch

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

Sidebar

Related Questions

Consider the following code and assume that list is an synchronized List. List list
Consider the following code. It's an anchor tag with an id called leader-module-total that
Consider the following code: int i = 3 << 65; I would expect that
Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
Sometimes JavaScript doesn't make sense to me, consider the following code that generates a
Consider the following code (from a requirement that says that 3 is special for
Consider the following code sample that uses MEF to create an object of type
Consider the following code, the first demonstrates that the cleanup executes when we're finished
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
Consider the following code: def CalcSomething(a): if CalcSomething._cache.has_key(a): return CalcSomething._cache[a] CalcSomething._cache[a] = ReallyCalc(a) return

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.