首页 \ 问答 \ Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)

Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)

我将一个页面对象“$ 100.99”作为一个字符串捕获。 有没有办法将它转换为浮点数或BigDecimal为“100.99”?

我试过xyz.scan(/\d+/).join().to_i但是删除了小数。


I am capturing a page object “$100.99” as a string. Is there way to convert this to a float or BigDecimal as “100.99”?

I tried xyz.scan(/\d+/).join().to_i but that removes the decimal.


原文:https://stackoverflow.com/questions/27871906
更新时间:2024-05-04 22:05

最满意答案

使用单独的线程池是很好的默认实践,并且共享线程池是一种(可能是过早的)优化。

通过Java 7,答案是否定的,没有默认线程池,并且建议有许多线程池。 这是一种很好的分离方式,可以防止一组任务中的阻塞行为干扰另一组任务。

如果你分享线程池,你应该问问题如下:

  • 日志框架能够区分任务吗? (线程是区分的一种方式。)
  • 如果任务池A意外地请求过多的线程中断,应该任务池B是否饿死? 当您注意到任务池B失败时,您是否能够诊断任务池A中的问题?
  • 如果A池阻止B饿死?

也许你创建了一个像LightweightThreadpool一样的东西。 你写的前5个任务以轻量级的方式使用它。 而第六项任务......的确如此,除了它还将错误写入磁盘,并且这些错误出人意料地大,有时还有很多错误,并且它们不会受到限制。 突然之间,前5项任务被迫挨饿,不知道是什么打击了他们,而且,当你写这些任务时,你确信他们是安全的,可能没有为这类事件做好准备。

所以共享线程池就好了,因为在同一台服务器上运行两个不同的进程是可以的。 您应该首先仔细考虑资源管理,并明白现在这些任务是资源耦合的。 缺少默认线程池会试图强制默认使用单独的线程池,并在共享之前仔细考虑这些问题。

从Java 8开始,答案是“是”(根据Tagir对此问题的回答 )。 但是你会注意到,如果你向该线程池提交阻塞任务,一切都将开始可怕的失败。


Using separate threadpools is good, default practice, and sharing threadpools is a (possibly premature) optimization.

Through Java 7 the answer is no, there is not a default threadpool, and the recommendation is to have many threadpools. It's good separation and will prevent blocking behavior on one collection of tasks from interfering with another.

If you share threadpools you should ask questions like:

  • will the logging framework be able to distinguish tasks? (Threads is one way to distinguish.)
  • If task pool A accidentally requests way too many threads and gets cut off, should task pool B starve? When you notice task pool B is failing will you be able to diagnose the problem in task pool A?
  • If pool A blocks should B starve?

Maybe you create something like a LightweightThreadpool. And the first 5 tasks you write use it in a lightweight fashion. And the 6th task... does, except it also writes errors to disk, and those errors are surprisingly big, and sometimes there's many of them, and they're not throttled. Suddenly the first 5 tasks are starved and have no idea what hit them, and furthermore, when you wrote those tasks, you really believed they were secure and might not have prepared for this type of incident.

So sharing threadpools is about as okay as having two different processes run on the same server is okay. You should think about resource management very carefully first and understand that the tasks are resource-coupled now. The lack of a default threadpool is trying to force you to use separate ones by default, and think about these questions carefully before sharing one.

As of Java 8 the answer is "yes" (per Tagir's answer on this question). But you will notice everything will start horribly failing if you submit blocking tasks to that threadpool.

相关问答

更多
  • 你可以将你自己的ThreadFactory传递给ScheduledThreadPoolExecutor 。 你的ThreadFactory将创建线程,并可以给它任何你想要的名字。 您的ThreadFactory也可以重用Executors.defaultThreadFactory() ,并且只在返回线程之前更改名称。 You can pass your own ThreadFactory to ScheduledThreadPoolExecutor. Your ThreadFactory will crea ...
  • 具有synchronized块的回调机制可以在这里有效地工作。 我之前在这里回答过类似的问题。 有一些限制(参见链接的答案),但它足以简单地跟踪正在发生的事情(良好的可维护性)。 我已经调整了源代码并使其更有效地适用于大多数任务将并行执行的情况(因为n和m很大),但有时必须是串行的(当任务是针对网格G的同一点时) )。 import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.ReentrantL ...
  • 我不一定会说它本身就是劣质设计,但需要考虑一些事情。 就个人而言,我喜欢线程池,如果使用正确,它是一个强大的解决方案。 性能下降的原因是,当固定池大小为2时,一次只能运行两个线程。 有可能你需要一个更大的数字 - 可能是10,可能是100 - 但至少你可以设置它,以便你不会耗尽内存。 也就是说,如果原始设计内存不足,因为创建的线程太多,你肯定需要一次限制活动线程的数量,所以你的性能会受到一些打击...它只是不需要是全有或全无。 您还可以使用无限的执行程序线程池,假设正在运行的线程确实及时返回,这样您仍然不会 ...
  • 这是一种竞争条件。 如果你遵循submit()足够长的时间(在源代码中),你将到达ThreadPoolExecutor.execucte() : public void execute(Runnable command) { if (command == null) throw new NullPointerException(); /* long comment block removed */ int c = ctl.get(); if (workerCo ...
  • 使用单独的线程池是很好的默认实践,并且共享线程池是一种(可能是过早的)优化。 通过Java 7,答案是否定的,没有默认线程池,并且建议有许多线程池。 这是一种很好的分离方式,可以防止一组任务中的阻塞行为干扰另一组任务。 如果你分享线程池,你应该问问题如下: 日志框架能够区分任务吗? (线程是区分的一种方式。) 如果任务池A意外地请求过多的线程中断,应该任务池B是否饿死? 当您注意到任务池B失败时,您是否能够诊断任务池A中的问题? 如果A池阻止B饿死? 也许你创建了一个像LightweightThreadpo ...
  • Java Akka插件( play.libs.Akka )转发到Scala插件( play.api.libs.Akka ),后者又根据应用程序的配置启动一个新的Actor系统。 (这就是所有的插件。) 因此,您可以使用基于akka配置项的普通application.conf文件来配置ActorSystem及其所有调度程序(调度程序也是ExecutionContext)。 这些调度程序是文档所指的线程池。 导入play.api.libs.concurrent.Execution.default时使用默认线程池 ...
  • 我想你需要这样的东西: // initialization of your threads Runnable[] runnables = new Runnable[5]; for (int i=0; i<5; i++) { runnables[i] = new MyRunnable(i); } // initialization of thread pool with fixed size 3 ExecutorService executor = Executors.newFixedThreadP ...
  • 你有没有试过看过Cyclic Barrier 。 它经过优化,允许一组线程停止并等待每个人都达到共同的障碍。 我没有看到任何理由为什么它不能与已知数量的汇集线程一起使用,并引用了公共屏障。 如果您需要在使用障碍await()计数调用的回调上进行同步,可能会有一些额外的复杂性,因为它在不同的线程中执行。 Have you tried looking at a Cyclic Barrier. It is optimized to allow a group of threads to stop and wait ...
  • 为什么此池不允许使用现有池中的线程子集来支持有限的并发性,同时仍然避免每次都创建新线程? Thread和ThreadFactory API不允许您回收任意Thread对象。 问题是ThreadFactory::newThread需要一个实现来返回一个给定Runnable的线程,但Thread API只允许由Thread构造函数设置Runnable 。 在Thread修复此问题会破坏模型。 (替换已启动的线程的Runnable意味着什么。) 理论上可以通过定义Thread的子类来修复,其中实际的run()方法 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。