为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 firestore web 中将值保存为双精度值?

如何在 firestore web 中将值保存为双精度值?

宝慕林4294392 2022-12-29 14:03:30
我已经尝试过提出的问题,但我没有找到正确的答案,我试图将一个值添加为双精度值,但将数据添加为字符串。这是我的 JavaScriptvar Price = 0.0;const priceValue = document.getElementById("price").value;Price = priceValue;database.collection("product").doc(uuidv4()).set({            price: Price,        });这是我的 html<div class="inputfield">          <label>Price</label>          <input id="price" type="number" class="input">       </div>
查看完整描述

1 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

问题与风暴无关,而是与您对输入数据的处理有关,您会看到输入字段的值始终是字符串类型,如下面的代码片段所示


function myFun() {

  const priceValue = document.getElementById("price").value;

  console.log(priceValue, typeof priceValue)

}

<div class="inputfield">

  <label>Price</label>

  <input id="price" type="number" class="input" value="0.0">

  <button onClick="myFun()">Check type</button>

</div>


因此,为了在 firestorm 中实际将值保存为数字,您首先需要手动解析它。


function myFun() {

  const priceValue = parseFloat(document.getElementById("price").value);

  console.log(priceValue, typeof priceValue)

}

<div class="inputfield">

  <label>Price</label>

  <input id="price" type="number" class="input" value="0.0">

  <button onClick="myFun()">Check type</button>

</div>


<input>number 类型的元素用于让用户输入数字。它们包括内置验证以拒绝非数字条目,但是元素的真实值仍然是一个字符串。


查看完整回答
反对 回复 2022-12-29
  • 1 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号