Handling international currency input in Ruby on Rails

前端 未结 5 1842
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 10:06

I have an application that handles currency inputs. However, if you\'re in the US, you might enter a number as 12,345.67; in France, it might be 12.345,67

5条回答
  •  遇见更好的自我
    2021-02-01 10:30

    You could give this a shot:

       def string_to_float(string)
    
          string.gsub!(/[^\d.,]/,'')          # Replace all Currency Symbols, Letters and -- from the string
    
          if string =~ /^.*[\.,]\d{1}$/       # If string ends in a single digit (e.g. ,2)
            string = string + "0"             # make it ,20 in order for the result to be in "cents"
          end
    
          unless string =~ /^.*[\.,]\d{2}$/   # If does not end in ,00 / .00 then
            string = string + "00"            # add trailing 00 to turn it into cents
          end
    
          string.gsub!(/[\.,]/,'')            # Replace all (.) and (,) so the string result becomes in "cents"  
          string.to_f / 100                   # Let to_float do the rest
       end
    

    And the test Cases:

    describe Currency do
      it "should mix and match" do
        Currency.string_to_float("$ 1,000.50").should eql(1000.50)
        Currency.string_to_float("€ 1.000,50").should eql(1000.50)
        Currency.string_to_float("€ 1.000,--").should eql(1000.to_f)
        Currency.string_to_float("$ 1,000.--").should eql(1000.to_f)    
      end     
    
      it "should strip the € sign" do
        Currency.string_to_float("€1").should eql(1.to_f)
      end
    
      it "should strip the $ sign" do
        Currency.string_to_float("$1").should eql(1.to_f)
      end
    
      it "should strip letter characters" do
        Currency.string_to_float("a123bc2").should eql(1232.to_f)
      end
    
      it "should strip - and --" do
        Currency.string_to_float("100,-").should eql(100.to_f)
        Currency.string_to_float("100,--").should eql(100.to_f)
      end
    
      it "should convert the , as delimitor to a ." do
        Currency.string_to_float("100,10").should eql(100.10)
      end
    
      it "should convert ignore , and . as separators" do
        Currency.string_to_float("1.000,10").should eql(1000.10)
        Currency.string_to_float("1,000.10").should eql(1000.10)
      end
    
      it "should be generous if you make a type in the last '0' digit" do
        Currency.string_to_float("123,2").should eql(123.2)
      end
    

提交回复
热议问题