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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:23:11+00:00 2026-05-21T19:23:11+00:00

I have fields hr and min , both integers in my application. For hr

  • 0

I have fields hr and min, both integers in my application. For hr field, if the user enters “1” I would like Rails to automatically pad it to “01” before saving it to the database. Also for the min field if the user enter “0” it should put in as “00”.

How can I do this?

  • 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-21T19:23:12+00:00Added an answer on May 21, 2026 at 7:23 pm

    It’d be better to store it as an integer and just display it as you described on runtime. Every language has its own way to pad zeros – for Ruby you can use String#rjust. This method pads a string (right-justified) so that it becomes a given length, using a given padding character.

    str.rjust(integer, padstr=' ') → new_str

    If integer is greater than the length of str, returns a new String of length integer with str right justified and padded with padstr; otherwise, returns str.

    some_int = 5
    some_int.to_s.rjust(2, '0')  # => '05'
    some_int.to_s.rjust(5, '0')  # => '00005'
    
    another_int = 150
    another_int.to_s.rjust(2, '0') # => '150'
    another_int.to_s.rjust(3, '0') # => '150'
    another_int.to_s.rjust(5, '0') # => '00150'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When designing user table what would be the must have fields from the security/user
I have 2 fields in the database month (numeric) and year (numeric) and I
I have two fields in a table. One contains values such as BTA, BEA,
I have a few tables that have similar fields but not exactly the same.
I have a basic ActiveRecord model in which i have two fields that i
Assume you have some objects which have several fields they can be compared by:
Some of my classes have final fields that are populated from the constructor as
i am building a webapp that will have notes fields all over the place,
I have the following fields: Inventory control (16 byte record) Product ID code (int
I have some char() fields in a DBF table that were left encrypted by

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.