Rounding xs:double value

Hi in bea xquery function pallette we only foung round() for decimal type.But we had a requirement to round xs:double values.
How can i achive this ,any method for round double values or to convert double to decimal.

does round( $someDouble ) not work?
Can you please try a few things yourself before posting?
If you already have, can you please post what you have tried?

Similar Messages

  • Need some help in Rounding a double value to a whole number

    Hey Peeps,
    Need some help here, I got a method that returns a value in double after a series of calculation.
    I need to know how can I round the double value, so for example,
    1. if the value is 62222.22222222, it rounds to 62222 and
    2. if the value is 15555.555555, it rounds to 15556
    How can i do this
    Zub

    Hi Keerthi- Try this...
    1. if the value is 62222.22222222, it rounds to 62222 and
    double d = 62222.22222222;long l = (int)Math.round(d * 100); // truncatesd = l / 100.0;
    double d = 62222.22222222;
    System.out.println(d);
    long l = (int)Math.round(d * 100);
    // truncatesSystem.out.println(l);
    d = l / 100.0;System.out.println(d);
    for (int i = 0; i < 1000; i++)
    {    d -= 0.1;}
    for (int i = 0; i < 1000; i++)
    {    d += 0.1;}System.out.println(d);
    regards- Julie Bunavicz
    Output:
    62222.22222222
    62222
    62222.22
    62222.22000000000001

  • Round a double value to a specific number of decimal places?

    Hello,
    Is there standard java function which will round a double value to a specified number of decimal places? Something like:
    double d = 4.34523;
    d = round(d, 2);
    where d is finally assigned the value of 4.34?
    Thanks!
    -exits

    No, because doubles hold values in binary (as do all values in a computer, of course, but there's no additional stuff to indicate decimal values).
    If you want values with specific rounding rules in decimal, use java.math.BigDecimal.
    If you just want to format the number with a specified number of decimal digits, use java.text.DecimalNumber.

  • Rounding a double value

    What is the easiest way to round a Double variable to 2 significant figures?
    Cheers Mike

    Oh, classical and very typical question on this forum.
    The short answer is nope. You don't do that and you don't need to do that. If you really really have to must doing that, you used the wrong type - double.
    By definition, double is an approximation. No gurantee on precision lose.
    --lichu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • WPF Data gird double value in cell round up automatically

    Hi All,
      I have one datagrid where one of the columns contains double values. I placed limitations on length and decimal part. Decimal part is always comes with 2 digits. Now during editing the cell that holds double value in DataGrid , if I enter value of
    99.99 then it is becoming 100. At this moment the cell is still under edit mode.
     If the columns hold decimal type values then this automatic round up is not happening. But due to some constraints , at this moment we can't change the columns type from  double to decimal. So I just want to disable the auto round up with double
    values in DataGrid. I searched in MSDN forums but did not find any , tried adding StringFormat for columns. But it did not help.
    I tested by keeping the double value outside of the DataGrid ( in editbox ) , I don't see any automatic round up. So I suspect there could be something with DataGrid that is resulting automatic round up.
    Is there any way to disable this automatic round op on double values in DataGrid? I welcome your comment.
    Thanks,
    Brahmaji.

    Well, 99.99 as you mentioned in your original post is not the same value as 9999999999999999. You cannot store the value 9999999999999999 in a double field.
    You could change the type to decimal to be able to store values with a higher precision:
    public class Movie
    public string Title { get; set; }
    public int Year { get; set; }
    public string Director { get; set; }
    public bool Hit { get; set; }
    public decimal Price { get; set; }
    new Movie()
    Title = "The Lawnmower Man",
    Year = 1992,
    Director = "Brett Leonard",
    Hit = true,
    Price = 22.23M
    If you want to prevent the value of the source property from getting set when an invalid double value is entered you could implement your own ValidationRule:
    namespace SampleGrid
    class MyValidationRule : System.Windows.Controls.ValidationRule
    public override System.Windows.Controls.ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    string s = value.ToString();
    double d;
    if(double.TryParse(s, out d))
    return new System.Windows.Controls.ValidationResult(false, "invalid value");
    return System.Windows.Controls.ValidationResult.ValidResult;
    <DataGridTemplateColumn Header="Price">
    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding Price, StringFormat=##.00}"/>
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate xmlns:local="clr-namespace:SampleGrid">
    <TextBox>
    <TextBox.Text>
    <Binding Path="Price" StringFormat="##.00">
    <Binding.ValidationRules>
    <local:MyValidationRule ValidationStep="RawProposedValue"/>
    </Binding.ValidationRules>
    </Binding>
    </TextBox.Text>
    </TextBox>
    </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
    <!--<DataGridTextColumn Header="Price"
    Binding="{Binding Price, StringFormat=##.00}" />-->
    Then the value won't get rounded. Of course you can still not set the double source property to 9999999999999999 though.
    There is no property that you can set on the DataGrid to prevent the value from getting rounded.
    Hope that helps. 
    Please remember to helpful posts as answer to close the thread and then start a new thread if you have a new question.

  • Double value truncated to two decimal places without rounding the value.

    I want to truncate double value to two decimal places without doing the rounding of the value.
    Is there any method which can directly do the truncation.

    There's many ways to achieve this such as using
    BigDecimal's setScale method or type-casting. This is
    the way I like to do it:double d = -5.239;
    d = d > 0 ? Math.floor(d * 100) / 100.0 : Math.ceil(d
    * 100) / 100.0;
    Your division by 100 may cause an rounding error, because there are numbers which no finite binary representation. That's splitting hairs! I know ;-)

  • Rounding up doubles

    Just wondering if anyone knows how to round up a double so that for instance 100.232323 can be up to 100.
    This is due to trying to convert a price in pence to a meaningful price.
    Also is there anyway to represent a figure in pounds and pence???
    Regards.

    If you are working with money you shouldn't do floating point arithmetic. You should either use one of the following:
    1. java.math.BigDecimal. See example: double monthlyInterest = 5.5d / 1200d;
    double monthlyPayment = 700.25d;
    BigDecimal interestAmount = new BigDecimal(monthlyInterest).setScale(5,BigDecimal.ROUND_HALF_UP);
    BigDecimal principalAmount = new BigDecimal(monthlyPayment).setScale(2,BigDecimal.ROUND_HALF_UP);
    BigDecimal appliedPayment = payment.subtract(interestAmount).setScale(2,BigDecimal.ROUND_HALF_UP);
    //to get their double values just do (for example)
    double i = interestAmount.doubleValue();
    double p = principalAmount.doubleValue();
    double payment = appliedPayment.doubleValue();2. Multiply all numbers by a power of 10 (for example 100) and then truncate or round the decimals, then do your arithmetic, then divide by the same number. To truncate, just cast your double value to an int.int newValue = (int) someDoubleValue;To round, you can use any of the java.lang.Math methods.Math.ceil(someDoubleValue);
    Math.floor(someDoubleValue);
    Math.round(someDoubleValue);You can do your own research. :)
    The reason why you should not do floating point arithmetic is because it is not accurate.

  • Round up double to 2 decimals

    How to round up double to 2 decimals? where in API i should look up? Thank you for your help!
    e.g., Book (including tax) :14.99 + 14.99 * 10% = 16.489 ---> 16.49

         * Scale decimal number via the rounding mode BigDecimal.ROUND_HALF_UP.
         * @param value Decimal value.
         * @param scale New scale.
         * @return Scaled number.
         * @since 1.8.3
        static public double getScaled(double value, int scale) {
            double result = value; //default: unscaled
            //use BigDecimal String constructor as this is the only exact way for double values
            result = new BigDecimal(""+value).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            return result;
        }//getScaled()

  • Fast scaling of double values?

    So far, I use my own method to scale double values:
         * Scale decimal number via the rounding mode BigDecimal.ROUND_HALF_UP.
         * @param value Decimal value.
         * @param scale New scale.
         * @param roundingMode Optional rounding mode from BigDecimal.ROUND_... constants. (Default: BigDecimal.ROUND_HALF_UP)
         * @return Scaled number.
         * @since 1.8.3
        static public double getScaled(double value, final int scale, final int... roundingMode) {
            double result = value; //default: unscaled
            value = (Double.isNaN(value) || Double.isInfinite(value)) ? 0.0 : value; //reser NaN
            //use BigDecimal String constructor as this is the only exact way for double values
            int rm = BigDecimal.ROUND_HALF_UP;
            if (roundingMode != null && roundingMode.length > 0) {
                rm = roundingMode[0];
            result = new BigDecimal(""+value).setScale(scale, rm).doubleValue();
            return result;
        }//getScaled()BigDecimal offers scale methods, so I use them. The String constructor is also the only way to convert a double into a BigDecimal.
    This works fine but is quite slow on heavy number crunching with lots of scales. Is there a faster way?

    After heavy profiling of our number crunching apps, I replaced my former scaling method (with BigDecimal) with a costum version:
         * Get scaled decimal value with a fixed rounding method 'ROUND_HALF_UP'.
         * <p>
         * Note: there's also a getScaled() version that supports more rounding modes but is slower.
         * @param value Value to scale.
         * @param scale New scale.
         * @return Scaled values.
         * @since 2.14.0
         * @see #getScaled(double, int, int) Scaling with custom rounding mode.
        static public double getScaled(double value, final int scale) {
            double result = 0.0; //default: unscaled
            if (value != 0.0 && !Double.isNaN(value) && !Double.isInfinite(value)) {
                final BigDecimal bd = new BigDecimal(""+value);
                final int signum = bd.signum();
                final long l = bd.unscaledValue().abs().longValue();
                final int s = scale - bd.scale();
                if (s < 0) { //new scale is smaller than old scale
                    long l2 = l / (long) Math.pow(10.0, -s); //cut old unrequired scale
                    final long roundDigit = (l / (long) Math.pow(10.0, -s -1)) % 10;
                    if (roundDigit >= 5) {
                        l2 +=1;
                    result = l2 / Math.pow(10.0, scale);
                } else { //new scale is equal or greater than old scale
                    result = (l * Math.pow(10.0, s)) / Math.pow(10.0, scale);
                if (signum == -1) {
                    result = -result;
            }//else: Nan/Infinite => 0.0
            return result;
        }//getScaled()And even though it looks a bit scary, it make the whole application more than twice as fast (4 Minutes compared to 9 Minutes with the former BigDecimal scaling). As our app does lots of different calculations with lots of interim scaling and storing, this performance gain is quite surprising, as it shows that BigDecimal is not very usefull for massive math calculations ...

  • Rounding of double numbers in J2ME

    Hi,
    I have a number like 25.1253345663. How do I round it to 25.1254 for a mobile application. Does J2ME have a library for this. Can I use J2SE libraries?
    Thanks

    Use this class
    If it is ok, say ok. :)
    public class Utils
    public static String formatDouble(double value, int decimals)
    String doubleStr = "" + value;
    int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")
    : doubleStr.indexOf(",");
    // Decimal point can not be found...
    if (index == -1) return doubleStr;
    // Truncate all decimals
    if (decimals == 0)
    return doubleStr.substring(0, index);
    int len = index + decimals + 1;
    if (len >= doubleStr.length()) len = doubleStr.length();
    double d = Double.parseDouble(doubleStr.substring(0, len));
    return String.valueOf(d);
    }

  • How to keep 2 decimal places for double value

    Hi,
    I need rounding function, which should round a double number till 2 digits, if there is no decimal part it should append 2 zero's in decimal places.
    eg. 100 --- > 100.00
    200.234 --> 200.23.
    I tried with Number class. but it is returning string, from that string if I try to convert to double value, it is rounding off one digit.
    Can any one help on this problem.
    Thanks,
    Suman.ch

    I am using xmlBeans using that I need to write this
    rounded value into a xml file, for that I need only
    double value with 2 decimal places.You can probably write the value as a String instead of a double somehow, so you can use the DecimalFormat class.
    You could try their mailing-lists for users/developers:
    http://xmlbeans.apache.org/community/index.html

  • Round a double to 2 descimal points

    I have a double value and want to round it to 2 descimals.
    eg. 123.454 to 123.45
    How do i do this
    Thanks

    I have a double value and want to round it to 2
    descimals.
    eg. 123.454 to 123.45double d = 123.454;
    double e = ( (double) Math.round(d*100))/100;

  • Rounding a double

    Hi,
    What's the best (and fastest since I got lots of values) way to perform a rounding on a double value?
    I think what I'd like to do is half-up rounding, so when it should round up to 2 digits e.g., it should cut off the rest when the third position is a 0;1;2;3 or 4 and should add one to the second digit, when the third is one out of them: 5;6;7;8;9.
    I tried it myself this way. Now this has happened - at the end (converting int to double) it screws at some values - why and how can I fix that or what is a better way to do this, since this solution looks kind of goofy to me?
        public double roundValue( double dTemp, int iDigits){
             // splitting up into the pure int part of the NOT rounded value
             System.out.println("****   dTemp to round: " + dTemp);
             System.out.println("iDigits to round to: " + iDigits);
              int iIntPart = (int) dTemp;
              dTemp -= (double) iIntPart;
              System.out.println("iIntPart: " + iIntPart);
              System.out.println("dTemp - only minor digits: " + dTemp);
              // calculation of a factor for the lower digits
              double dDigits = 1.0;
              for(int cnt = 0; cnt < iDigits; ++cnt)
                  dDigits *= 10.0;
              System.out.println("dDigits: " + dDigits);
              System.out.println("dTemp * dDigits: " + (dTemp * dDigits));
              System.out.println("Math.round: " + Math.round(dTemp * dDigits));
              System.out.println("Math.round/dDigits: " + ((Math.round(dTemp * dDigits)) / dDigits));
              // processing Math.round()
              dTemp = (Math.round(dTemp * dDigits)) / dDigits;
              System.out.println("dTemp - after: " + dTemp);
              // dTemp is now rounded
              dTemp += (double) iIntPart;
              /* here sometimes, the value returns results like this - why?
               * 4.941519877284079 -> iDigits == 2 -> 4.9399999999999995
               * 5.94004582533272 -> iDigits == 2 -> 5.9399999999999995
              System.out.println("****   dTemp after adding the int part: " + dTemp);
              return dTemp;
        }

    Thanx I tried it out and I thought I had to use Math.round() the way I did. But still what happens at the conversion from an int to a double - just curious?
    I did it now this way - I thought Math.round has some probs, with the digits in front of the point:
        public double roundValue(double iValue, int iDecimalPlace) {
            double dPowerOfTen = 1;
            while (iDecimalPlace-- > 0){
               dPowerOfTen *= 10.0;
            return (Math.round(iValue * dPowerOfTen) / dPowerOfTen);
         }

  • Subtracting very small double value

    i'm having trouble figuring this out..i'm sure it's a simple solution..anyhoo..i'm trying to run the following line of code
    value=1-((1-b1[8+(i%classes)])*(1-b2[4+(i%classes)]));
    let me point out that value is a double value, and b1 and b2 are double arrays...
    the values of b1 and b2 are very small(around E-60)
    the PROBLEM is that value returns 0.0 when i run my code instead of the small value. I'm sure it's something I'm doing/not doing, but I'd appreciate some help on what is going on. Also as a note, I'm running J2SE 1.3.1. I saw on the API that the min double value is supposed to be around E-324 but any suggestions people? Thanks!!

    1-b1[8+(i%classes)]This is 1 - (10^-60), right? A double value only has about 16 significant digits, not 60, so the nearest possible double number to that is 1. Hence you get 1 - 1*1. There's a whole school of mathematics called "numerical analysis" that deals with questions like this. In this particular case, if b1[x] and b2[x] are always extremely small, you should evaluate the expression in advance so that it looks like b1 + b2 - b1*b2, which in turn will round to b1 + b2.
    PC&#178;

  • Unable to display double values in Excel sheet using JExcel API

    Hi
    I am writing code to generate report in the form of Excel Sheet using JExcel API.
    Everything is going fine but whenever I want to put some double values in a cell it is only showing 2 decimal places. My problem is "I want to show upto five decimal places".
    Any kind of reply might help me lot.
    Thank U.

    If you enable the submit zero option, it still happens? This is a new feature on the display tabl
    #NumericZero Enhancements
    To display a numeric zero in place of an error message, you can enter #NumericZero in any of the three Replacement text fields. When you use the #NumericZero option:
    · Excel formatting for the cell is retained.
    · All calculations with dependency on the cell will compute correctly and will take the value of this cell as zero.
    · This numeric zero is for display only. When you submit, the zero value is NOT submitted back to the data source.
    You cannot set display strings for cells that contain an invalid member or dimension name (metadata error). Metadata errors produce standard descriptive error messages.
    Errors are prioritized in the following order from highest to lowest. The error message for a higher-priority error takes precedence over that for a lower-priority error.
    1. (Highest) Metadata errors
    2. #No access
    3. #Invalid/Meaningless
    4. #No data\Missing

Maybe you are looking for

  • Can I run two copies of FrameMaker at the same time?

    I am trying to learn Structured FrameMaker. Is it possible/legal/safe to start two copies of FrameMaker on my PC at the same time? Specifically I want to have one copy open in Structured FM to experiment and the other copy in Unstructured to take not

  • How to  change host name entry for java instance using config tool

    Hi Gurus, We are trying to invoke DR for dual stack SAP PI system.Strategy followed is BR Tool  backup and restore method. We are able bring up ABAP stack and it is still pointing to production Java stack. Could any one please guide us with step by s

  • IMovie and Cinema mode = MAJOR headaches!

    Hi there. I've been having this issue for some time now and while I've read similar topics on multiple forums (including this one) I have yet to find a viable fix. My problem is importing video from my Panasonic mini DV cam after shooting in Cinema m

  • Regarding limits in oracle 8

    Hey guys, i need your experience here;) We use oracle 8, and today some customers reported that regarding a table (financial one... ouch) all the entries they did since the 23rd october dont appear, the lines are blank , but incremented is there a li

  • SM19 Transaction in Background

    Hi ,    The transaction SM19 is currently executed on a specified time manually. The requirement is to write a program so that it can be scheduled and  made to execute automatically . The program executes correctly online , but does not work  when it