0

For example, I have a time data with string format:

00:25:23;16

I want to convert it to BigDecimal and tried:

a = '00:25:23;16'.to_d
=> #<BigDecimal:96cb548,'0.0',9(18)>

When I check a:

a.floor
=> 0

It looks not the true value. Then how to convert it the right way?

Addition

I expect the bigdecimal value like this(Maybe not a right value):

1543.123
s_zhang
  • 847
  • 2
  • 8
  • 16

1 Answers1

2

Assuming the ;16 means milliseconds then maybe you are looking for this?

> str = "00:25:23;16"
=> "00:25:23;16"
> h, m, s, ms = str.split(/[:;]/).map(&:to_f)
=> [0.0, 25.0, 23.0, 16.0]
> h * 3600 + m * 60 + s + ms/1000
=> 1523.016
Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • Thank you very much. It is what I want. Thought it looks a little complexity, I can use it now:) – s_zhang Dec 07 '16 at 05:36
  • ...and use [BigDecimal::new](https://ruby-doc.org/stdlib-2.3.0/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-new) to return a Big Decimal: `require 'bigdecimal'; BigDecimal.new(h * 3600 + m * 60 + s + ms/1000) #=> #`. – Cary Swoveland Dec 07 '16 at 06:57
  • Sorry, here `;16` means timecode. So do you know how to change it to second? – s_zhang Dec 09 '16 at 07:44
  • can anybody explain how to do it in java? – Diganta Jan 22 '17 at 10:55