How to get the result from fraction without rounding up in Thymeleaf - numbers

I am trying to get the relation between the height and width from image and then multiply by a fixed width to resize a picture using Thymeleaf.
For it, I saved the height and width in a java object called g. Using a fixed width: 270px I want to scale the height using the relation between the sized saved doing that:
th:style="'height:' + ${(g.height / g.width) * 270} +';
The saved and original size is: int height = 286 and int width = 423.
So the fraction's result should be 286/423 = 0.67.
But the problem is that this fraction g.height / g.width is giving me 0 as result.
How can I obtain decimals from this fraction? I tried with {#numbers.formatDecimal()} but not results.

Since both width & height are integers, it's doing integer division. Either store them as float/double on the object itself, or convert them to doubles in the expression.
th:style="'height:' + ${((0.0 + g.height) / g.width) * 270} +'px;'"

Related

how to make horizontal padding in iOS danielgindi charts

I'm using the ios-charts library and I would like to add some horizontal padding to my line charts so that the line does not start immediately at the border of the graph.
This is my current chart:
but I would like the blue line to have some padding as shown below. The rest should remain as it is. The reference gray lines should still take the entire width as they currently do.
I found it. This "padding" is actually ruled by the chart.xAxis.axisMinimum and chart.xAxis.axisMaximum. Those values are automatically set to the data min x and max x.
So if I want a left padding I just have to set a chart.xAxis.axisMinimum
In my case, I want around 10% of the x values to be padded, so I calculate it as
// dates is an array of Date representing my x values
if let maxX = dates
.map(\.timeIntervalSince1970)
.max(),
let minX = dates
.map(\.timeIntervalSince1970)
.min() {
let spanX = maxX - minX
let padding = spanX * 0.1
let axisMinimum = minX - padding
// set the left padding
chart.xAxis.axisMinimum = axisMinimum
}

Get first number of type CGFloat

I have following numbers in CGFloat
375.0
637.0
995.0
I need to get the first number in CGFloat data type. For example the result for #1 must be 3.0, for #2 must be 6.0 and #3 must be 9.0
I tried the following
let width:CGFloat = 375.0
// Convert Float To String
let widthInStringFormat = String(describing: width)
// Get First Character Of The String
let firstCharacter = widthInStringFormat.first
// Convert Character To String
let firstCharacterInStringFormat = String(describing: firstCharacter)
// Convert String To CGFloat
//let firstCharacterInFloat = (firstCharacter as NSString).floatValue
//let firstCharacterInFloat = CGFloat(firstCharacter)
//let firstCharacterInFloat = NumberFormatter().number(from: firstCharacter)
Nothing seems working here. Where am I going wrong?
Update
To answer #Martin R, find below my explanation
I am implementing a grid-view (like photos app) using UICollectionView. I want the cells to be resized based on screen size for iPhone/iPad, Portrait and Landscape. Basically I don't want fixed columns. I need more columns for larger screen sizes and lesser column for smaller screen sizes. I figured that perhaps I can decide based on screen width. For example if the screen width is 375.0 then display 3 columns, If somewhere around 600 then display 6 columns, if around 1000 then display 10 columns and so on with equal width. So what I came up with is a) decide columns based on first number of the screen size and then for width divide by actual screen width. For example for a screen width of 375.0 I will have a cell size of CGSize(width: screenWidth / totalColumn) and so on.
You said:
For example if the screen width is 375.0 then display 3 columns, If somewhere around 600 then display 6 columns, if around 1000 then display 10 columns and so on with equal width.
So what you really want is not the first digit of the width (which would
for example be 1 for width = 1024 instead of the desired 10)
but the width divided by 100 and rounded down to the next integral value:
let numColumns = (width / 100.0).rounded(.down)
Or, as an integer value:
let numColumns = Int(width / 100.0)
var floatNum:CGFloat = 764.34
var numberNeg = false
if floatNum < 0{
floatNum = -1.0 * floatNum
numberNeg = true
}
var intNum = Int(floatNum)
print(intNum) //764
while (intNum>9) {
intNum = Int(intNum/10)
}
floatNum = CGFloat(intNum)
if numberNeg {
floatNum = -1.0 * floatNum
}
print(intNum)//7
print(floatNum)//7.0
try this one ...I hope it'll work

iText7 Bug: Endless loop in ImageRenderer

i came across an endless-loop-issue due to floating point arithmetic in iText7 DotNet ImageRenderer.
I've added an image with 2464x3692 pixels to an PDF page area of 523x770 pixels, causing the Layout() function to loop endlessly because every iteration returned LayoutResult.NOTHING.
The main issue is that the image height is calculated inside the Layout-function using the following statement:
Line 90: height = (float)width / imageWidth * imageHeight;
Due to the floating point operation the height got a value of something like 770.0001.
This is greater than the area height, causing the loop to return LayoutResult.NOTHING, so the image gets autoscaled again.
Inside the AutoScale-function only the image width is checked, which IS inside the area boundaries and so nothing is scaled.
I've fixed this issue by changing the following code inside ImageRenderer.cs:
90 - height = (float)width / imageWidth * imageHeight;
90 + float? propHeight = RetrieveHeight();
91 + height = propHeight == null ? (float)width / imageWidth * imageHeight : propHeight.Value;
214 - SetProperty(Property.HEIGHT, UnitValue.CreatePointValue(area.GetBBox().GetHeight()));
215 + SetProperty(Property.HEIGHT, area.GetBBox().GetHeight());
ImageRenderer now uses the height that is set inside AutoScale without recalculating it if not necessary.
Hopefully this will be fixed in one of the next releases. :)
Regards
Yosh

Focal length to Field of view

I'm importing some 3D models from Rhino 3D to Unity. When doing that I need to import the camera views. In Rhino they have the property Focal length and in Unity we have Field of view.
I need to convert focal length to Field of View. I found a formula to convert the values here
http://paulbourke.net/miscellaneous/lens/
I'm planning to use this formula
vertical field of view = 2 atan(0.5 height / focal length)
My question is how can I find the value for the height. I'm not sure from where I can get that in Unity.
Thanks
Maths isn't my strong suit but I do recall the formula was used in a certain BFBC2 fov tool
hFov = 2 * atan(tan( vFov/2 ) * width/height)
Where width and height are your current screen resolution dimensions.
I hope this is correct for your purpose.
To answer my question, this is the formula I ended up using at the end. Hope that'll be useful for anyone having a similar issue.
Code in C# in Unity
// Standard film size
int filmHeight = 24;
int filmWidth = 36;
// Formula to convert focalLength to field of view - In Unity they use Vertical FOV.
// So we use the filmHeight to calculate Vertical FOV.
double fovdub = Mathf.Rad2Deg * 2.0 * Math.Atan(filmHeight / (2.0 * focalLen));
float fov = (float) fovdub;

Changing this to a set pixel width

This is probably super easy, but I can't get it to work right. I am trying to do a simple update to my iOS app by changing an image resize option to a set pixel size. the code below is what i'm working with and how it is currently functioning:
selectedWidth = (30 * selectedWidth)/100;
selectedHeight = (30 * selectedHeight)/100;
I assume this is 30% of 100%? I just need the selected width to be 200px, and height to be automatic. Can somebody please help me? I'm a newb with this type of coding, my developer is away and i thought this would be an easy change haha.
So if I understand you right, I think this might help
selectedHeight = (selectedHeight / selectedWidth) * 200;
selectedWidth = newWidth;
This should calculate the correct height to your 200px width.
If you want to change your fixed-width from time to time, you can define a constant variable in the m.file below the imports:
#define kWidth 200
and use it instead of the hard coded 200 in the code on top.
Example:
old width: 100px
old height: 50px
new width: 200px
new height: (50 / 100) * 200 = 100px