如何在 Mathematica 中将包含小数点的字母数字(参考)数字转换为字符串 [英] How to Convert an Alphanumeric (Reference) Number Containing a Decimal Point to a String in Mathematica

查看:168
本文介绍了如何在 Mathematica 中将包含小数点的字母数字(参考)数字转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DAA76647.1 类型的参考编号,我想在 Mathematica 中将它原封不动地转换为字符串.

I have a reference number of the following type DAA76647.1 which I want to convert unchanged to a string in Mathematica.

那就是

myfn[DAA76647.1]

myfn[DAA76647.1]

作为输出给出

DAA76647.1"

"DAA76647.1"

有没有简单的方法可以做到这一点?(输入不能是字符串,除了转换为字符串,我不想以任何其他方式更改输入).

Is there an easy way to do this? (The input cannot be a string and, other than conversion to a string, I do not want to change the input in any other way).

更新

ToString /@ {A1234, 1234.1, A1234 .5}

给出以下输出(我只是从键盘输入了所有内容)

gives the following output (where I have simply entered everything from the keyboard)

{"A1234", "1234.1", "0.5 A1234"}

如果小数点前面是字母数字的话,好像有问题.

It appears that if what goes before the decimal point is alphanumeric, there is a problem.

可能的解决方法

根据 David Carraher 的建议解决方案,一种可能的方法如下:

Based on a suggested solution by David Carraher, a possible method is as follows:

ToString[# /.a_ b_ :> ToString[b] <> StringDrop[ToString[a], 1]] & /@ {A1234, 
  1234.1, A1234 .5}

作为输出:

{"A1234", "1234.1", "A1234.5"}

如果小数点后面的不是字母数字,并且前面的不是以零开头(例如 0A123.1),这似乎可以正常工作.

This seems to work OK provided that what comes after the decimal point is not alphanumeric, and provided that what comes before does not begin with zero (0A123.1, for example).

如果字母数字只出现在小数点后,则可以合并

If alphanumerics occur only after the decimal point, this may be incorporated

StringReplace[ToString[123.45 B55c], Whitespace -> ""]

但是如果字母数字出现在小数点前后,数字仍然需要作为字符串输入.

but if alphanumerics occur before and after the decimal point the number still needs to be entered as a string.

大卫卡拉赫的原始建议

f[Times[a_, b_]] := ToString[b] <> ToString[a] 

推荐答案

如果您通过 Mma 笔记本中的输入单元输入 DAA76647DAA76647.1,Mma 会将字符解释为乘法.当您输入时,它甚至会自动在 7 和 .1 之间插入一个空格(至少在 Mma 8 中).

If you enter DAA76647DAA76647.1 via an input cell in a Mma notebook, Mma will interpret the characters as a multiplication. It even automatically inserts a space between the 7 and the .1 (at least in Mma 8) when you input it.

DAA76647DAA76647 .1 // FullForm

(*Out= Times[0.1`,DAA76647DAA76647] *)

这看起来很有希望:

f[Times[a_, b_]] := ToString[b] <> ToString[a]


编辑:然而,正如 TomD 指出的(我不知何故错过了),它为解决方案添加了一个额外的零!


EDIT: However, as TomD noted (and I somehow missed), it adds an additional zero to the solution!

f[Times[DAA76647DAA76647 .1]]
(*Out= DAA76647DAA766470.1  *)

%//FullForm
"DAA76647DAA766470.1"

TomD 后来展示了如何通过 StringDropping 零来处理这个问题.

TomD later showed how it is possible to handle this by StringDropping the zero.

如果小数点右侧仅出现数字并且左侧部分未被解释为产品,则此更正后的解决方案将起作用.

This corrected solution will work if only numbers appear to the right of the decimal point and if the left-hand part is not interpreted as a product.

如果你尝试输入 DAA76647.01A Mma 会解析为

If you try to enter DAA76647.01A Mma will parse it as

(*Out= Times[".01",A,DAA76647]    *)

注意它改变了组件的顺序.

Notice that it changes the order of the components.

我看不到处理这种重新排序的方法.

I cannot see a way to handle this reordering.

这篇关于如何在 Mathematica 中将包含小数点的字母数字(参考)数字转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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