Any way to floor a float from 1.000000 to 1.000 ?

Post » Thu Jun 21, 2012 7:55 pm

I am trying to turn a number like this: 1.945753

into this: 1.945

or even this: 1.946

Is there a function or some mathematical process to do that in a script?
User avatar
Laura Elizabeth
 
Posts: 3454
Joined: Wed Oct 11, 2006 7:34 pm

Post » Thu Jun 21, 2012 12:48 pm

Try using http://www.creationkit.com/Floor_-_Math? Or http://www.creationkit.com/Ceiling_-_Math?
User avatar
Yonah
 
Posts: 3462
Joined: Thu Aug 02, 2007 4:42 am

Post » Thu Jun 21, 2012 10:43 am

Huh? wiki on this page http://www.creationkit.com/Math_Script said:


int Function http://www.creationkit.com/Floor_-_Math(float afValue)
  • Calculates the largestinteger less than or equal to the value.
I need a float not an integer.

1point945

not 1945

Is there a way to make numbers like 1point945787 into 1point945 or 1point946 ?

I do not want numbers like 1.945787969404 as notifications from my mod.

Try using http://www.creationkit.com/Floor_-_Math? Or http://www.creationkit.com/Ceiling_-_Math?
User avatar
Nomee
 
Posts: 3382
Joined: Thu May 24, 2007 5:18 pm

Post » Thu Jun 21, 2012 8:48 pm

Perhaps something like this?

Float Function RoundFloatToXDecimalPlaces(Float MyFloat, Int DecPlaces)    Int Multiplier = Math.Pow(10, DecPlaces)    MyFloat *= Multiplier    Int Floored = Math.Floor(MyFloat)    if (MyFloat - Floored >= 0.5)        Return ((Floored + 1) / Multiplier) as Float    else        Return (Floored / Multiplier) as Float    endifEndEvent
User avatar
Undisclosed Desires
 
Posts: 3388
Joined: Fri Mar 02, 2007 4:10 pm

Post » Thu Jun 21, 2012 11:17 pm

What you're looking for is called 'truncation.'

Random's code should work well for it.
User avatar
Chris Ellis
 
Posts: 3447
Joined: Thu Jul 26, 2007 10:00 am

Post » Thu Jun 21, 2012 8:18 pm

If you convert a number from float to integer, al floating part will be eliminated. Thus, if you as an example have the number 1.9392933332 and want to convert to something like 1.93 you can simly multiply the number by 100 convert to integer, convert again to float and divide by 100. You could also use this along floor and ceiling functions.
User avatar
keri seymour
 
Posts: 3361
Joined: Thu Oct 19, 2006 4:09 am

Post » Thu Jun 21, 2012 11:55 am

Funny as I did try that at first, but what I did not do was convert to an INT first.

MyFloat = MyFloat * 1000
MyFloat = floor(MyFloat)
MyFloat = MyFloat / 1000

did not work(that is when I posted here...)

So now I will try:


Int MyInt = (MyFloat * 1000) as Int
MyInt = floor(MyInt)
MyFloat = (MyInt / 1000) as float

Thanks!

If you convert a number from float to integer, al floating part will be eliminated. Thus, if you as an example have the number 1.9392933332 and want to convert to something like 1.93 you can simly multiply the number by 100 convert to integer, convert again to float and divide by 100. You could also use this along floor and ceiling functions.
User avatar
Brιonα Renae
 
Posts: 3430
Joined: Mon Oct 22, 2007 3:10 am

Post » Thu Jun 21, 2012 10:02 pm

I'd do it by using modulus ...

;Truncate to 3 decimalsmyFloat -= myFloat % 0.001;Round to 3 decimalsmyFloat += 0.0005myFloat -= myFloat % 0.001


Edit: Sorry but after trying this myself I acutally realized modulus in Papyrus wont work on non-integers. Limited !@#$%
User avatar
Dalley hussain
 
Posts: 3480
Joined: Sun Jun 18, 2006 2:45 am

Post » Thu Jun 21, 2012 9:42 pm

... would give a result that's 1000 times what's expected.
User avatar
Emily abigail Villarreal
 
Posts: 3433
Joined: Mon Aug 27, 2007 9:38 am


Return to V - Skyrim