Always Show 4 Decimal Places - xlwt

Is it possible to force a cell to always show 4 decimal places using xlwt? Regardless of the number actually present after the decimal place. I have done some searching and I can find a string to always reduce to 2 decimal places, but I need certain cells on my sheet to show 4.

Use xlwt styles. Here's a simple example:
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet("test")
decimal_style = xlwt.XFStyle()
decimal_style.num_format_str = '0.0000'
sheet.write(0, 0, 1.23456789, decimal_style)
sheet.write(0, 1, 1.01, decimal_style)
book.save('test.xls')
Or you can use xlwt.easyxf for defining styles. Hope that helps.

Related

Is there a Within Formula somewhere?

I am trying to determine if there is a specific string of data in a cell. so if have a cell that says 1 2 3 4 5 6. Using the Space as a delimiter. is there a way for me to query the 4 out of it? and get a return of the 4?
I have tried using search and find but thats are just giving me the len position within the cell.
So, use find(), which you say you did but did not show how...
What about:
=if(iferror(find(A1,B1,1),0)>0,A1,"Not found")
I've setup my sheet like so:
The formula works like so:
mid(A2, //grab a substring from the middle of A2
SEARCH("4",A2) //find the value "4" inside A2
,1) //make MID grab exactly one character (length of string "4"
With some jimmying, you can generalize this however you want.
try:
=REGEXEXTRACT(A1&""; "\b4")
Another solution for Google Sheets:
=if(iserror(MATCH(A1,SPLIT(B1," "),0)),"",A1)
(aka: look for A1 in B1)
enjoy

Google Sheets - Combine multiple IF Functions into one cell

I'm trying to produce a SKU in Google Sheets for a product using the values of three variants (Title, Colour and Size)
The product is 'Lightweight trainers' with colour variants of 'Red' and 'Blue', and the sizes range from 5 - 12.
Link to spreadsheet
https://docs.google.com/spreadsheets/d/1trq0X3MjR-n2THFnT8gYYlwKscnQavCeeZ8L-ifYaHw/edit?usp=sharing
Aim
I'm hoping to have a SKU that displays the product, the colour variant and the shoes size.
Example: LW-1-8 (Lightweight trainer, colour Red, size 8)
Product is Lightweight Trainers with a value of LW.
Colour variant 'Red' with a value of 1 and 'Blue' with a value of 2.
Shoe size variant = number ranging from 5 to 12.
Here's what I have so far, joining the colour and size variants.
=IFS(I2="Red",1,I2="Blue",2)&"-"& IFS(K2="5",5,K2="6",6,K2="7",7,K2="8",8,K2="9",9,K2="10",10,K2="11",11,K2="12",12)
However, I'm getting stuck in joining the data in column B with this function.
Any help with combining this data from multiple cells into one would be greatly appreciated.
TL;DR
=ARRAYFORMULA(IF(B2:B<>"", IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K,))
Answer
What you want is basically:
<title>-<color number>-<shoe size>
To convert this to a function we can split it into each part and take it step by step:
Step 1: Title
For the first part -the title- we need to match the value with the shorthand. A simple list in an IFS is enough.
IFS(B2="Lightweight Trainers", "LW")
Obviously for now it only has a single value (Lightweight Trainers) but you could add more:
IFS(B2="Lightweight Trainers", "LW", B2="Heavyweight Trainers", "HW")
Step 2: color number
Similar to the previous step, it’s a mapping using ifs:
IFS(I2="Blue", "-1", I2="Red", "-2")
The dash is added so when adding everything it will only have it if
Step 3: shoe size
In this case we can simply get the value:
K2
Step 4: Adding everything together
We only need to add it with the dashes in between:
=IFS(B2="Lightweight Trainers", "LW")&"-"&IFS(I2="Blue", 1, I2="Red", 2)&"-"&K2
Step 5: Extending for the entire column automatically
We will use ARRAYFORMULA to add a single formula to the first cell and get it automatically extended to the entire column. We first add it to the formula we already have, and then extend the ranges to the entire column:
=ARRAYFORMULA(IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K)
Remember to remove all the values in the column so array formula doesn’t override them (it would generate an error).
As you can see the formula generates errors for the rows that have no values. A good way of handling this case is to filter the rows without a title. In a single row would be:
=IF(B2<>"", [the entire formula],)
Notice the last comma.
So putting everything together and extending its range to the column, is:
=ARRAYFORMULA(IF(B2:B<>"", IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K,))
Adding this to N2 should work.
Final notes
It seems that you use 150 when the size it’s not a whole number. If you want to keep that functionality you may use:
IF(K2-int(K2)=0, K2, 150)
On the last component and expand it the same way.
You may also want to prevent having two dashes when a value is missing (LW-5 instead of LW--5). To do so, I’d recommend adding it to each component instead of the formula that adds them together.
References
IFS (Docs Editors Help)
IF (Docs Editors Help)
ARRAYFORMULA (Docs Editors Help)
try in N2:
=IFS(I2="Red",1,I2="Blue",2)&"-"&
IFS(K2=5,5,K2=6,6,K2=7,7,K2=8,8,K2=9,9,K2=10,10,K2=11,11,K2=12,12)
or use:
=IF(I2="red", 1, IF(I2="blue", 2, )&IF((K5>=5)*(K5<=12), "-"&K5, )

Can't understand shape(output) = (shape(value) - ksize + 1) / strides in TensorFlow docs

In the following excerpt from http://tensorflow.org/api_docs/python/nn.md#pooling
shape(output) = (shape(value) - ksize + 1) / strides
where the rounding direction depends on padding:
padding = 'SAME': Round down (only full size windows are considered).
padding = 'VALID': Round up (partial windows are included).
I can't understand the formula above. I am familiar with following formula though:
shape(out) = (shape(value) - ksize + 2*pad)/strides+1.
Are the two formulas equivalent?
For example, let's say shape(value) = 9, ksize = 3, strides = 2, and padding = 'SAME'.
In the first formula the shape(output) will be (9-3+1)/2 = 7/2 = 3.5 and rounding down results in 3.
In the second formula the shape(output) will be (9-3+2*1)/2 + 1 = 5
It doesn't seem to be the same formula. Even if I round up the first one, the result will be 4.
In addition to that, the padding definition seems to be inverted. Isn't the 'SAME' padding that includes partial windows?
I think I figured out the problem. The two formulas become equivalent if you assume that the appropriate padding is already included in shape(value). But I still think the definition of the padding types have been swapped in the documentation. I created an issue to report that: https://github.com/tensorflow/tensorflow/issues/196

Firefox submits number fields as decimals

Firefox seems to submit input fields of type number as decimals independant of its visible value (e.g.: visible value: 1, real value/posted data: 1.0).
My backend cannot handle it as it expects an integer.
But I still want to use the number type as it handles the keyboard layout on mobile devices.
I already tried to set the step attribute to 1 (which is default anyway).
Sorry, but you are stuck with this way of formatting number fields if you want to keep using that type of control.
What you can do is create a hidden input that is updated when the number input field changes. You can format the value you put in there the way you like to.
So in short, the best thing is to get your backend straight, but that might be out of your hands. Else you can use the workaround provided.
Well actually it’s not a bug; the form field is behaving as defined by the W3C.
Numeric input fields can take additional attributes “min” and “step”, which constrain the range of values allowed in your input.
This is because the default step is 1. So far, so obvious.
However, the step attribute also determines which values are valid, so a step of 1 means you can enter 1, 2, 3 etc. and a step of 2 means you can enter 2, 4, 6 etc, and when you click the up/down buttons the number will increase/decrease by 2 each time, but entering 3 or 5 in the box will cause a validation error. You can also use a decimal value: for example, a step of 0.3 will allow values such as 0.3, 0.6, 0.9 etc, but not 1 or 2.
I was completely wrong. I filled the input from my backend and it was a double value. When using the type integer it only submits "1".

How to get the real height of a character (fontmetrics/graphics2d)

I am currently in need to get the real height of a character. I am aware of the functions like getDecsent(), getAscent(), ... but they only allow to get values regarding the hole font (in its context). I also tried the way using getStringBounds(), but that is the same story.
Like the title says, I am looking for a way to get the height value of just one char at a time.
For example 'N' is higher then 'n', 'I' just a little bit higher then 'i' and so on
Thanks for your time
Use this
Rectangle2D bounds = font.getStringBounds("put your string here", context);
//font can be set to whatever you want
//my suggestion is that you use the same font for both characters
//context is a object of FontRenderContext
System.out.println(bounds.getHeight());
//Instead of just printing it you could do this a second time and compare the 2 strings