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

The Archive Base Latest Questions

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

Say I have this: class Account … property :charge, Decimal, :precision => 7, :scale

  • 0

Say I have this:

class Account
  ...
  property :charge, Decimal, :precision => 7, :scale => 2
  ...
  classy stuff
  ...

  def self.balance(prefix)
    x = Account.get(prefix.to_sym).order(:fields => [:charge]).sum(:charge)
    sprintf("%5.2f", x)
  end
end

(Edit: The value of all :charge fields is 0.13E2 (0.1E2 + 0.3E1). This is correctly returned. Only in a View does it seem to get borked from sprintf)

In IRB Account.balance(:AAA) returns => "13.00"

if I call Account.balance(:AAA) from a view I get TypeError at /accounts
can’t convert nil into Float

Account.balance(:AAA) works anywhere I call it except in a view. If I remove sprintf("%5.2f", x) I get 0.13E2 in my view. (using Account.balance(:AAA).to_f in a view gives me 13.0)

Is sinatra incompatible with sprintf? or am I not understanding how to use sprintf?

(Edit: This is the offending view:)

<section>
  <% @accounts.each do |account| %>
    <article>
      <h2><%= account.prefix %></h2>
      <span><p>This account belongs to <%= account.name %> & has a balance of $<%= Account.balance(account.prefix) %>.</p></span>
    </article>
  <% end %>
</section> 
  • 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-26T03:54:01+00:00Added an answer on May 26, 2026 at 3:54 am

    Wouldn’t it make more sense to define balance as an instance method rather than a class method? It looks from your example like you’re calling balance in an account-specific way anyway, so why not make it:

    # the model
    
    class Account
      #...
    
      def balance
        amount = self.order(:fields => [:charge]).sum(:charge)
        sprintf "%5.2f", amount
    
        # or the infix version:
        "%5.2f" % amount
      end
    end
    

    ,

    # the view
    
    ...balance of $<%= account.balance %>...
    

    I know that this doesn’t address sprintf per se, but the problem is more likely to be coming from the slightly convoluted lookup than from a built-in method. Even if my specific code doesn’t suit your application, it might be worth simplifying the lookup step, even if that involves a few more lines of code.

    The advantage of this approach is that there is no doubt that you’ll be getting the right Account record.

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

Sidebar

Related Questions

Say I have this class: public class Account { public int AccountID { get;
Say I have this class: class myclass { public int Field1{ get; set; }
say I have this class: class animal { function noise() { print 'woof'; }
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
I know python functions are virtual by default. Let's say I have this: class
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say i have this usercontrol public class test : UserControl { public int
say I have this control: public partial class bloc999 : UserControl { bloc999Data mainBlock
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar

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.