【问题标题】:how can I scale the the numbers in flutter我怎样才能缩放颤振中的数字
【发布时间】:2023-02-02 09:45:06
【问题描述】:

我必须将数字缩放到 0 到 9 之间,我怎么能在 flutter 应用程序中做到这一点?

我的数据是从 0.000001 到 >500,

我需要它是从 0 到 9

【问题讨论】:

  • 您能否提供输入示例及其相应的预期输出?

标签: flutter dart numbers scale scaling


【解决方案1】:

对于最小-最大归一化:

最小 = 0.000001 max= n(需要定义上限来代替>500)

新值=(值* 9)/(最大-最小)

【讨论】:

    【解决方案2】:

    最简单的方法是首先将数据规范化到 0-1 范围:

    final normalized = (data - min) / (max - min);
    

    然后乘以你的新最大值:

    final converted = normalized * 9;
    

    【讨论】:

      【解决方案3】:

      我正在寻找这个并创建了这个方法,我正在关注scale a number between a range

      /// Scale value between two different range
      double scaler(
        double value,
        double start1,
        double stop1,
        double start2,
        double stop2,
      ) {
        final result =
            ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2;
        return result;
      }
      

      和测试用例

      void main() {
        group('scaler', () {
          test("value 1.5, scale (0,3) to (0,10),  should return 5 ", () {
            final matcher = scaler(1.5, 0, 3, 0, 10);
            expect(5, matcher);
          });
          test("value 1.5, scale (0,3) to (-4.2, 6.7),  should return 1.25 ", () {
            final matcher = scaler(1.5, 0, 3, -4.2, 6.7);
            expect(1.25, matcher);
          });
        });
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多