抑制尾随的“."在 Mathematica 的数值输出中 [英] Suppressing a trailing "." in numerical output from Mathematica

查看:20
本文介绍了抑制尾随的“."在 Mathematica 的数值输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些直接的方法可以确保在转换为字符串时,近似数字(即带有 Real 头的数字)不会有尾随."?如果在实际显示小数部分的情况下只有小数点,我希望它.

Is there some straightforward way to ensure that, when converted to strings, approximate numbers (i.e., numbers with the Real head) won't have a trailing "."? I would like it if they were to only have the decimal point in cases where there's actually a displayed fractional part.

我发现的解决方案并不健壮,并且依赖于以一种尴尬的方式将 PrecisionAccuracy 一起使用 NumberForm,或者使用RealDigits 以一种更尴尬的方式.

The solutions I've found are not robust, and depend on using Precision and Accuracy together NumberForm in an awkward way, or using RealDigits in an even more awkward way.

提前致谢.

推荐答案

我过去在用数字显示数字时使用过这个:

I've used this in the past when displaying numbers in figures:

Integerise[x_] := If[Round[x] == x, ToString[Round@x] <> ".0", ToString@x]

只需删除 <>".0" 如果您不希望整数显示为零小数.

Just remove <> ".0" if you don't want integers to be displayed with a zero decimal.

更新:正如dreeves 在评论中提到的,ToString 仍然会截断一个整数的 0.0001 左右的数字并显示点.

Update: As mentioned by dreeves in the comment, ToString will still truncate a number within 0.0001 or so of an integer and display the dot.

删除尾随点的更好方法是对 ToString 使用 Inputform 格式:

A better way to remove the trailing dot is to use the Inputform format for ToString:

NormalNumber[x_] := ToString[x, InputForm]

测试:

NormalNumber /@ {5, 5.5, 123.001, 123.0001}

这可以合并到上面的 Integerise 中以解决所指出的问题.

This could be incorporated into Integerise above to fix the problem noted.

这篇关于抑制尾随的“."在 Mathematica 的数值输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆