2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: cf1391c1c11feddfaeb4bdef
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 创建一个小数
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/ca8GEuW'
|
|
|
|
forumTopicId: 16826
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: create-decimal-numbers-with-javascript
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
我们也可以把小数存储到变量中。 小数有时候也被称作<dfn>浮点数</dfn>或者 <dfn>floats</dfn>。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-07-15 07:34:11 +00:00
|
|
|
**提示:** 不是所有的实数都可以用浮点数(<dfn>floating point</dfn>)来表示。 因为可能产生四舍五入的错误, [点击这里了解细节](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems)。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
创建一个变量 `myDecimal`,并给它赋值一个浮点数(例如 `5.7`)。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`myDecimal` 应该是一个数字。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(typeof myDecimal === 'number');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`myDecimal` 应该包含小数点。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(myDecimal % 1 != 0);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})();
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const ourDecimal = 5.7;
|
2021-01-13 02:31:00 +00:00
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const myDecimal = 9.9;
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|