Step between 0 and 1 produces floating point rounding errors

已關閉
#742 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
react-native, typescript
領域
mobile-dev

研究方向

首先,在 iOS 和 Android 上執行提供的 Expo 重現,並使用 minimumValue 0、maximumValue 1 和 step 0.1。追蹤 onValueChange 接收計算值的位置,然後確認發出的值和顯示的標記使用預期的小數步長,且沒有浮點數瑕疵。

由索引模型根據 Issue 內容生成。

描述

bug report v6

Environment

System:
OS: macOS
Binaries:
Node: v22.16.0
npm: 10.9.2

are you using the new architecture?
No (Expo-managed workflow)

which version of react & react-native are you using?
React: 19.1.0
React Native: 0.81.5
@react-native-community/slider: 5.0.1
Expo SDK: 54.0.17

Description

when using minimumValue of 0, maximumValue of 1, and a step of 0.1, the value returned by onValueChange shows floating-point precision errors. Instead of returning decimal values (e.g. 0.4), it returns e.g. 0.4000000596046448, this can be seen in the value and the markers. Occurs consistently across iOS and Android in the Expo environment.

Reproducible Demo

import React, { useState } from 'react';
import { View, Text } from 'react-native';
import Slider from '@react-native-community/slider';

export default function App() {
  const [value, setValue] = useState(0);

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Slider
        style={{ width: 300 }}
        minimumValue={0}
        maximumValue={1}
        step={0.1}
        value={value}
        onValueChange={(val) => setValue(val)}
      />
      <Text>Value: {value}</Text>
    </View>
  );
}

Potential Fix

Round the computed value before emitting it in onValueChange: value = parseFloat(value.toFixed(6));

Workaround

For a temp workaround users can use:
onValueChange={(val) => setValue(Math.round(val * 10) / 10)}

主要語言
TypeScript
星號
1.4k
分支
297
平均合併
1 天 11 小時
30 天內合併 PR
10

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

callstack/react-native-slider 的其他 Issue

查看 callstack/react-native-slider 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。