Smalltalk中如何将数字转换为字符串(视觉作品) [英] How to convert a number to a string in Smalltalk (visual works)

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

问题描述

我很难找到有关如何在 Cincom Smalltalk 中获取数字的字符串表示的信息.这是如何在这种语言中执行的?具体来说,我正在编写一个对象的字符串表示,类似于 Java 中的 toString 函数.

I am having difficulty finding information on how to get a string representation of a number in Cincom Smalltalk. How is this performed in this language? Specifically I'm composing a string representation of an object, similar to the toString function in Java.

推荐答案

printString 方法旨在返回一个字符串以允许程序员解释该数字.如果您在示例中使用了 3.14d(双精度型),您会在 printString 中看到d"字符.

The printString method is meant to return a string to allow a programmer to interpret the number. If you used 3.14d in your example (a Double), you would see the 'd' character in the printString.

这对开发人员来说很好,但如果您要构建供其他人使用的用户界面,则应该使用 PrintConverter.举个例子:

This is fine for developers but if you're building a user interface for other people to use, you should use a PrintConverter. Here's an example:

(PrintConverter for: #number withFormatString: '$#,###.##;[RED]($#,###.##);$0.00')
formatStringFor: -35675.389d

如果您检查结果,您会看到它是括号中的红色文本(因为它是负数)并四舍五入到两位小数并带有美元符号.这是为用户界面显示的一种更好的数字.有关格式化字符的信息,请参阅 NumberPrintPolicy 的类注释.

If you inspect the result, you'll see that it's red text in parentheses (because it's negative) and rounded to two decimal places with a leading dollar sign. This is a much better kind of number to display for a user interface. For information on the formatting characters, see the class comment for NumberPrintPolicy.

这篇关于Smalltalk中如何将数字转换为字符串(视觉作品)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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