小编Ger*_*man的帖子

在swift中将科学记数法改为十进制

我想一个浮点值从转换1e-050.00001斯威夫特.

我使用了以下扩展名:

extension Float {
    var avoidNotation: String {
      let numberFormatter = NumberFormatter()
      numberFormatter.allowsFloats = true
      numberFormatter.maximumFractionDigits = 8
      numberFormatter.numberStyle = .decimal
      return numberFormatter.number(from: "\(self)")!.stringValue
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用浮点值时,1e-05.avoidNotation结果却是一样的0.00001

swift

3
推荐指数
1
解决办法
851
查看次数

Rspec格式将参数发布到String值

当我发送一个对象json时,里面的所有字段都改为字符串,在控制器中断开我的验证,我得到以下错误.

Api::V1::BillsController POST #create when logged in 
     Failure/Error: post :create, { bill: bill_attributes }
     Apipie::ParamInvalid:
       Invalid parameter 'value' value "41.64794235693306": Must be Float
     # ./app/controllers/concerns/exception_aspects.rb:4:in exception_wrapper
     # ./spec/controllers/api/v1/bills_controller_spec.rb:135:in block (4 levels) in <top (required)>
Run Code Online (Sandbox Code Playgroud)

我尝试的测试表明 request.headers['Content-Type'] = 'application/json'

let(:bill_attributes) { FactoryGirl.attributes_for :bill }

before(:each) do
   request.headers['Content-Type'] = 'application/json'
   post :create, { bill: bill_attributes }
end

it "when is valid description" do
    expect(json_response[:description]).to eq(bill_attributes[:description])
end
Run Code Online (Sandbox Code Playgroud)

我的工厂是

FactoryGirl.define do
 factory :bill do
  description { FFaker::Lorem.phrase }
  value { (rand() * 100).to_f } …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rspec2 rspec-rails

2
推荐指数
2
解决办法
2131
查看次数

标签 统计

rspec ×1

rspec-rails ×1

rspec2 ×1

ruby ×1

ruby-on-rails ×1

swift ×1