#javascript #radix #string-representation #floating-point #numbers #convert #section

radix-ecmascript

将浮点类型转换为字符串表示,该表示由 ECMAScript 语言规范第 9.8.1 节定义

1 个不稳定版本

0.1.0 2023 年 8 月 23 日

#1879 in 算法

自定义许可

27KB
219

radix-ecmascript

允许将浮点类型(f32f64)转换为基数表示,就像在 ECMAScript 中一样(例如 (0.5).toString(16)(0.5).toString(36) 等)。

该库实现了 ECMAScript 语言规范第 9.8.1 节 "应用于数字类型的 ToString"。

添加依赖项

cargo添加 radix-ecmascript

示例

use radix_ecmascript::ToRadixStr;

fn main() {
    println!("{}", (0.123).to_radix_str(16).unwrap());
}

这将打印 0.1f7ced916872b。同样可以通过在 JavaScript 中调用 (0.123).toString(16) 来实现。

注意,在这个例子中,我们解包了 Result<String, InvalidBaseError>。在实际情况下,你应该有适当的错误传播,但是了解 to_radix_str 只会在给定的基数超出有效范围时返回 InvalidBaseErrorradix_ecmascript::MIN_BASEradix_ecmascript::MAX_BASE),所以如果你传递的是常数,可以安全地解包错误。

贡献

在进行贡献时,请确保您的pull request中的贡献是合理的。同时,您还需要在您创建的任何新文件中包含版权模板。以下模板适用于JetBrains IDE,但您可以根据需要手动添加标题(如果有必要)并进行少量修改。您还应添加您自己的名字。

Copyright (c) $originalComment.match("Copyright \(c\) (\d+)", 1, "-", "$today.year")$today.year Levi-Michael Taylor. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This library implements logic found in Google's open-source V8 library, specifically:
* double.h (https://github.com/v8/v8/blob/f83601408c3207211bc8eb82a8802b01fd82c775/src/numbers/double.h)
* DoubleToRadixCString (https://github.com/v8/v8/blob/f83601408c3207211bc8eb82a8802b01fd82c775/src/numbers/conversions.cc#L1269)
Copyright 2014, the V8 project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.
* Neither the name of Google Inc. nor the names of its
   contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This license can also be found at: https://github.com/v8/v8/blob/f83601408c3207211bc8eb82a8802b01fd82c775/LICENSE

无运行时依赖