Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Round A Number To The First Decimal
Started by oromanetti Apr 09 2013 02:26 AM

* * * * * 1 votes
6 replies to this topic
round number decimal
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

oromanetti

[GLOBAL: userInfoPane.html]
oromanetti
  • Contributor

  • 234 posts
  • Corona SDK

Hi,

 

I would like to round some values.

Example : 5.5324343434   >>  5.5

 

I don't want to round to the nearest integer (floor or ceil math function)

What I want to do, is only round to the first decimal like on these examples :

5.34343434 >> 5.3

2.9877939 >> 2.9

8.583002002 >> 8.5

 

Thank you for your help

Olivier



[TOPIC: post.html]
#2

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

Simple enough:

 
local function roundToFirstDecimal(t)
    return math.round(t*10)*0.1
end

 

C



[TOPIC: post.html]
#3

oromanetti

[GLOBAL: userInfoPane.html]
oromanetti
  • Contributor

  • 234 posts
  • Corona SDK

Thank you Caleb :)

So simple!

Olivier



[TOPIC: post.html]
#4

mdbonneaux

[GLOBAL: userInfoPane.html]
mdbonneaux
  • Observer

  • 27 posts
  • Corona SDK

I'd like to also say thanks Caleb.  You just made a small portion of my project a bit easier and I'll take every bit I can get.



[TOPIC: post.html]
#5

john.cressman

[GLOBAL: userInfoPane.html]
john.cressman
  • Contributor

  • 101 posts
  • Corona SDK

Thanks Caleb... that helped me!



[TOPIC: post.html]
#6

Satheesh

[GLOBAL: userInfoPane.html]
Satheesh
  • Contributor

  • 732 posts
  • Corona SDK

A more generalized round function..

local function roundToNthDecimal(num, n)
  local mult = 10^(n or 0)
  return math.floor(num * mult + 0.5) / mult
end

 



[TOPIC: post.html]
#7

FearTec

[GLOBAL: userInfoPane.html]
FearTec
  • Contributor

  • 218 posts
  • Corona SDK

Stash, I use that too.

 

function RoundNumber(num, idp)

    local mult = 10^(idp or 0)
    return math.floor(num * mult + 0.5) / mult
end



[topic_controls]
[/topic_controls]