我想一个浮点值从转换1e-05到0.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
当我发送一个对象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)