本文介绍了红宝石战略转换浮点数与语言学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在写我认为是一个简单的.rb文件来将浮点数转换为字符串。该字符串以字的形式返回我的浮点数。所以如果我有11.11,那么我会有11美元和11美分。到目前为止,我已经扩展了运行良好的float类。如何将11美分转换为11美分,我遇到了麻烦。 en.numwords将踢倒十一点一。我想过试一试哈希来解决我的问题,11 => 11美分。任何想法如何我可以实现这一点?也许更好的方式来实现这一点?



这是我到目前为止:

  require'rubygems'
require'linguistics'
语言学::使用(:en)



类浮动
def to_test_string
puts self #check
puts self.en.numwords
self.en.numwords
end

end

放入输入两个伟大的浮点数加法
放置第一个数字
c = gets.to_f
放置第二个数字
d = gets.to_f
e = c + d
#puts e
puts e.to_test_string
puts输入一个很大的浮点数!例10.34
a = gets.to_f
#把
放置a.to_test_string

感谢您的帮助!发布一些代码,以便我可以尝试点子!

解决方案

这个问题可以通过将float分成两个值来解决:美元。

  require'rubygems'
require'linguistics'
Linguistics :: use(:en)

类浮动
def to_test_string
放置自我#check

#分割为美元和美分
美分=自%1
美元=自 - 美分b $ b美分=美分* 100

text =#{dollars.to_i.en.numwords}美元和#{美分至1千万分语}美分

放文本
文本
结束
结束

puts输入两个很好的浮点数加法
puts第一个数字
c = gets.to_f
putsSecond number
d = gets.to_f
e = c + d
#puts e
puts e。 to_test_string
puts输入一个很大的浮点数!例10.34
a = gets.to_f
#放一个
puts o_test_string


So I'm writing what I thought was a simple .rb file to convert a float number into a string. The string returns my floating point number in words. So if I have 11.11 then I would have eleven dollars and eleven cents So far I've extended the float class which has worked alright. I'm having trouble with how to convert the 11 cents into eleven cents. en.numwords would kick back eleven point one one. I've thought about trying out a hash to solve my problem where 11=>eleven cents. Any thoughts how I could implement this? Perhaps a better way to implement this?

Here's what I have so far:

 require 'rubygems'
 require 'linguistics'
 Linguistics::use( :en )



 class Float
 def to_test_string
 puts self #check
 puts self.en.numwords
 self.en.numwords
 end

 end

 puts "Enter two great floating point numbers for adding"
 puts "First number"
 c = gets.to_f
 puts "Second number"
 d = gets.to_f
 e = c+d
 # puts e
 puts e.to_test_string
 puts "Enter a great floating number! Example 10.34"
 a = gets.to_f
 # puts a
 puts a.to_test_string

Thanks for the help! Post some code so I can try ideas out!

解决方案

This problem can be solved by splitting the float into two values: dollars and cents.

require 'rubygems'
require 'linguistics'
Linguistics::use( :en )

class Float
    def to_test_string
        puts self #check

        #Split into dollars and cents
        cents = self % 1
        dollars = self - cents
        cents = cents * 100

        text = "#{dollars.to_i.en.numwords} dollars and #{cents.to_i.en.numwords} cents"

        puts text
        text
    end
end

puts "Enter two great floating point numbers for adding"
puts "First number"
c = gets.to_f
puts "Second number"
d = gets.to_f
e = c+d
# puts e
puts e.to_test_string
puts "Enter a great floating number! Example 10.34"
a = gets.to_f
# puts a
puts a.to_test_string

这篇关于红宝石战略转换浮点数与语言学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:44