This is an automated email from the ASF dual-hosted git repository.

harbs pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new e7fd3eab61 Added toDecimals utility function
e7fd3eab61 is described below

commit e7fd3eab619838e7e0778e4bec7090b8e7669653
Author: Harbs <[email protected]>
AuthorDate: Sun Aug 20 22:01:18 2023 +0300

    Added toDecimals utility function
---
 .../projects/Core/src/main/royale/CoreClasses.as   |  1 +
 .../org/apache/royale/utils/number/toDecimals.as   | 50 ++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as 
b/frameworks/projects/Core/src/main/royale/CoreClasses.as
index 3b451f72dc..51f3e792fc 100644
--- a/frameworks/projects/Core/src/main/royale/CoreClasses.as
+++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as
@@ -365,6 +365,7 @@ internal class CoreClasses
 
        import org.apache.royale.utils.number.pinValue; pinValue;
        import org.apache.royale.utils.number.getPercent; getPercent;
+       import org.apache.royale.utils.number.toDecimals; toDecimals;
 
        import org.apache.royale.utils.event.hasPlatformModifier; 
hasPlatformModifier;
        import org.apache.royale.utils.sendEvent; sendEvent;
diff --git 
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/number/toDecimals.as
 
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/number/toDecimals.as
new file mode 100644
index 0000000000..89ba4baa90
--- /dev/null
+++ 
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/number/toDecimals.as
@@ -0,0 +1,50 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.utils.number
+{
+               /**
+        *  Rounds a number to the specified precision (the number of digits in 
a
+        *  decimal number). The precision can range from 0 to 28. If you omit 
this
+        *  parameter, Math.round() rounds to the nearest integer.
+        *
+        *  @param value The number to round.
+        *
+        *  @param precision The number of decimal places to which to round. If 
you
+        *  omit this parameter, Math.round() rounds to the nearest integer.
+        *
+        *  @return The rounded value.
+        *
+        *  @example
+        *  <pre>
+        *  trace(MathUtil.round(3.14159, 3)) // 3.142
+        *  trace(MathUtil.round(3.14159, 2)) // 3.14
+        *  trace(MathUtil.round(3.14159, 1)) // 3.1
+        *  trace(MathUtil.round(3.14159, 0)) // 3
+        *  trace(MathUtil.round(3.14159, -1)) // 0
+        *  trace(MathUtil.round(314159, -2)) // 314200
+        *  trace(MathUtil.round(314159, -3)) // 314000
+        *  trace(MathUtil.round(314159, -4)) // 310000
+        *  </pre>
+        */
+       public function toDecimals(value:Number, precision:int = 0):Number
+       {
+               var decimalPlaces:Number = Math.pow(10, precision);
+               return Math.round(decimalPlaces * value) / decimalPlaces;
+       }
+}
\ No newline at end of file

Reply via email to