I want to extract the decimal part of a number and use it as a column name. For example, extract “001“ from 0.001. Can I use decimalFormat for this?
Please refer to the script below using DolphinDB:
x=1.001
decimalFormat(x - floor(x), "0.000").substr(2)
The result is 001.
Related
I am trying to format a column in a dataframe using style.
So far I successfully used the styling for a fixed number of decimals:
mytable.style.format('{:,.2f}', pd.IndexSlice[:, ['Price']])
but I need to expand this to formatting based on value as this:
if value is >=1000, then format to zero decimal places
if value is between 1000 and 1, then format to two decimal places
if value is < 1, then format to five decimal places
Does anyone have a solution for this?
Thank you!
Building upon #Code_beginner's answer – the callable should return formatted string as output:
def my_format(val):
if val >= 1000:
return f"{val:,.0f}"
if val >= 1:
return f"{val:,.2f}"
return f"{val:,.5f}"
mytable.style.format({'Price': my_format})
What you are looking for is called "conditional formatting". In which you set the conditions like you described and return the right format. There are examples in the documentation, they used a lambda function there. But you can also create a normal function which might look something like this:
def customfunc(val):
if val>=1000:
format='{:,.0f}'
if val<1000 and val>=1:
format='{:,.2f}'
if val<1:
format='{:,.5f}'
return format
df.style.format({0:customfunc})
This should style your first column like described in your problem. If the columns has a name you have to adjust it accordingly. If you have trouble see the documentation linked abve there are more examples.
Just to have it visually clear, this is how it looks now:
That's my line of code:
df.style.format({'Price': customfunc})
I used String.format() to round to the third decimal place. However this didn't work and I solved it using DecimalFormat.
Is there anything I implemented wrong?
val value = 23.695f
Timber.e("format: ${"%.2f".format(value)}")
Expect: 23.70
Result: 23.69
In your string formatting example you're not rounding the number you're just taking the first two decimal places. So your code drops the .005 and keeps the .69
If .70 is the desired result then DecimalFormat sounds correct to me
I want to use the today() function as a dynamic filter to show MM/YYYY as number format
So i want my filter to show =201601
But with my function i get 20161: input(CATS(Year(TODAY()),Month(TODAY())), 6.) =20161
Does anybody know how to show 201601 instead of 20161?
There is a format that will display a data in YYYYMM format.
put(today(),yymmn6.)
Did you intend to convert to a number instead of a character string? If so then you could use the INPUT function as in your example.
input(put(today(),yymmn6.),6.)
You could even use the YYMMDDN format if YYMMN doesn't work for you, just read the first 6 digits.
input(put(today(),yymmddn8.),6.)
Or you could build the number arithmetically.
year(today())*100+month(today())
Here's the proper function:
put(today(), yymmn6.)
I've got a column called Amount, with a lot of numbers looking like this:
67000.00000000000000000000
Some of the columns have 2 numbers after the decimal that need to be retained.
Which should amount to $67,000.00
But my problem is, when I format it into currency or numbers, I get MUCH larger numbers than i would like, looking like this:
6.700.000.000.000.000.000.000.000,00
How can I get it into the right format?
Edit: For this scenario, the user was using ACC2013 and the Field Type was Short Text. The method of conversion that succeeded was : CCur(Val(FieldNameHere))
CCur(YourFieldName)
This will convert it to a currency format.
CLng(YourFieldName)
This will convert it to a long integer format. (It will cut off the decimals)
If you're looking for a reference, Microsoft has a few examples and goes into brief detail about some of these conversion functions.
CCur(Replace("67000.00000000000000000000", ".", Format(0, ".")))
You have to replace point symbol to actual decimal separator before conversion. Because you can't know actual seprator, choosen in regional settings, you have to find it out - and such Format() operation does dirty work.
I am writing a class for handling decimal numbers, e.g. "123.456". I want one function for extracting the digits before the decimal point (123), and one function to extract the digits after the decimal point (0.456). My question is not how to do the programming, but how to name the functions? Do you have any better idea than digits_before_point() and digits_after_point()?
integralPart() and decimalPart() or integers() and decimals()
How about integral_part and decimal_part?
integer_part() and fraction_part()