开发者

Extract 10 digits after decimal point and apply rounding up to 3 postions

I want to implement logic of round开发者_JAVA技巧ing up to 3 decimal positions after rounding.

If the value of decimal place 1 – 3 is equal to 000 (regardless of the whole number), and the value of the 4th thru the 10th decimal place is greater than 0, the 3rd decimal place in the display will round up.

example:

1.1230000000  --> 1.123
1.1230010000  --> 1.123
1.1230600000  --> 1.124
1.0000010000  --> 1.001
1.0003000000  --> 1.003
5.0000001234  --> 5.001

looking forward


Math.Ceiling(myNumber*1000)/1000;

should do the trick for positive numbers. Test if you get the desired behavior for negative numbers (not given in your examples). If not, use Floor instead of Ceiling for negatives.


I am not a sharepoint person, but I have general languages experience.

not Math.ceil() use js Int() or whatever int() function you have. ceil() regardless of frac value != 0 always moves up to next integer. parseInt chops off the frac part, which is what he was really trying to do. I can't find any sharepoint API references on msdn. I am basing this on the JavaScript/ecmascript 262 5.1 spec. if you want number of digits like 5 digits, it's

Function truncateDigits(Double n, Integer numdigits)) 
    return Int(Math.Power(10,numdigits)*n)/Math.Power(10,numdigits);
EndFunction

but be aware that floating point has a problem with even repeatedly adding or subtracting 0.1 and you get wild results due to floating point error. to work with financials, you should use a decimal data type if there is one, the decimal data type if the language provides it does not have this problem. otherwise, you can simulate fixed-point fractional by looking at a large integer type and faking a decimal point in the middle or where you want it and calculating accordingly. display would require that you create your own display routines. but it would be fast and limited in abilities (no trig funcs, powers you would have to write on your own based on integers). you would need to come up with your own arithmetic routines, because normal ones assume the decimal point in the wrong place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜